Skip to content

Latest commit

 

History

History
120 lines (112 loc) · 3.34 KB

README.md

File metadata and controls

120 lines (112 loc) · 3.34 KB

Simple chat

Another one example of text messages exchange service. Based on the next libs list:

  1. ASP.NET Core 2.0;
  2. Yeoman generator;
  3. SignalR 1.0.0-preview1-final for ASP.NET Core 2.0;
  4. Angular 4.1.2;
  5. Typescript 2.7.2;
  6. Swagger 2.2.0 with Autorest API client codegen.

How to

To implement SignalR usage on ASP.NET Core app you should:

  1. Create ASP.NET Core project:
dotnet new angular
  1. Download NPM packages:
npm install
  1. Add NuGet references to the next libs:
    1. Microsoft.AspNetCore.All;
    2. Microsoft.AspNetCore.SignalR.
  2. Implement nested from Hub or Hub<T> class:
using System;
using System.Threading.Tasks;

using Microsoft.AspNetCore.SignalR;

using SimpleChat.Models;

namespace SimpleChat.Hubs
{
	public sealed class ChatHub: Hub<IChatHub>
	{
		public async Task Send(ChatMessageModel chatMessage)
		{
			if (chatMessage == null) throw new ArgumentNullException(nameof(chatMessage));

			await Clients.All.SendAsync(chatMessage);
		}
	}
}
  1. Add support for the SignalR in Startup.cs. Enable SignalR for our application:
services.AddSignalR();

Create route mapping for our SignalR's chat Hub implementation:

app.UseSignalR(routes =>
	// Note: chat hub name should start with '/'
	routes.MapHub<ChatHub>("/chathub")
);

Note:: hub name should start with '/'. 6. Install the latest version of the ASP.NET Core SignalR NPM package:

npm install @aspnet/signalr
  1. Now we can create, setup and start SignalR's HubConnection in a Typescript component:
this.chatHub = new HubConnection(originUrl + "/chathub");

this.chatHub.on(
  "Send",
  data => this.receiveMessage(data));

this.chatHub
  .start()
  .catch(error => console.log(error));
  1. Profit! That is all. Now we can start our chat:
dotnet run

Authentication

Note: Windows authentification is used by default.

Swagger

  1. Install Swagger NuGet package:
Install-package Swashbuckle.AspNetCore
  1. Initalize Swagger UI module in your Startup.cs Configure() method:
app.UseSwagger();
app.UseSwaggerUI(c =>
{
	c.SwaggerEndpoint("/swagger/v1/swagger.json", "Chat API V1");
});
  1. Enable XML documentation generation in your project settings;
  2. Specify Swagger generation settings in Startup.cs class ConfigureServices() method:
services.AddSwaggerGen(c =>
{
	c.SwaggerDoc(
		"v1",
		new Info
		{
			Title = "SimpleChat API",
			Version = "v1"
		});

	var basePath = PlatformServices.Default.Application.ApplicationBasePath;
	var xmlPath = Path.Combine(basePath, "SimpleChat.xml");
	c.IncludeXmlComments(xmlPath);
});
  1. OpenAPI SimpleChat API definition can be found:
https://localhost:44395/swagger/

Useful links