зеркало из
1
0
Форкнуть 0
AzureSignalR-samples/samples/ReliableChatRoom
Wanpeng Li a9699ea16d
update readme (#169)
2022-06-01 11:17:12 +08:00
..
Hub The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
Images The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
MessageHandler The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
Properties upgrade to .net 6.0 for some samples (#186) 2022-06-01 11:10:18 +08:00
SessionHandler The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
wwwroot The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
.gitignore upgrade to .net 6.0 for some samples (#186) 2022-06-01 11:10:18 +08:00
Dockerfile upgrade to .net 6.0 for some samples (#186) 2022-06-01 11:10:18 +08:00
Program.cs Set up the base frame of ReliableChatRoom sample (#108) 2019-09-18 14:17:46 +08:00
README.md update readme (#169) 2022-06-01 11:17:12 +08:00
ReliableChatRoom.csproj upgrade to .net 6.0 for some samples (#186) 2022-06-01 11:10:18 +08:00
ReliableChatRoom.sln.DotSettings.user The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
Startup.cs The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00
appsettings.json The reliable chatroom sample (#110) 2021-12-08 14:27:30 +08:00

README.md

Azure SignalR Service Advanced Chat Room

Just like ChatRoom sample, you have learned how to use Azure SignalR Service to build a chat room application. This sample demonstrates a chat room with more general features in a chat application. In this tutorial, you'll learn how to use Azure Storage Service and Azure Web App Service to get everything running in the cloud.

Now the sample supports:

  • Broadcast
  • Joined / Left Broadcast
  • New message notification
  • Send to User in a private chat room
  • Replay history messages

Provision storage service for data

We support the dependency injection (DI) software design pattern for the storage of messages and sessions.

Store the data in memory

Sometimes we don't need to store data in a real database (For example, in a test environment.) You should register the service in the app's Startup.ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
	...
	services.AddSingleton<ISessionHandler, InMemorySessionStorage>();
	services.AddSingleton<IMessageHandler, InMemoryMessageStorage>();
}

Store the data in Azure Table

If you don't have an Azure Storage Account, start now to create one for your project.

In Startup.ConfigureServices method, instead of registering InMemorySessionStorage and InMemoryMessageStorage, you need to use AzureTableSessionStorage and AzureTableMessageStorage and pass in connection string to make the application connect to the service.

public void ConfigureServices(IServiceCollection services)
{
	...
	services.AddSingleton<ISessionHandler, AzureTableSessionStorage>();
	services.AddSingleton<IMessageHandler, AzureTableMessageStorage>();
}

Now set the connection string in the Secret Manager tool for .NET Core, and run this app.

dotnet restore
dotnet user-secrets set ConnectionStrings:AzureStorage "<Your Azure Storage's connection string>"
dotnet user-secrets set Azure:SignalR:ConnectionString "<Your Azure SignalR's connection string>"
dotnet run

Quick Deploy via Docker image

You can also deploy this sample via existing docker image

docker run -e Azure__SignalR__ConnectionString="<signalr-connection-string>" \
           -e STORAGE_CONN_STRING="<storage-connection-string>" \
		   -p 5000:80 mcr.microsoft.com/signalrsamples/reliablechatroom:latest

Use your own database

If you want to use your own database to store the messages and sessions, you should create a class which implements ISessionHandler and another one implementing IMessageHandler.

Then, register your services in Startup.ConfigureServices like above and run the app

When you open http://localhost:5000, you can see the application using the configured storage services.

Publish your application

  1. Create a web app service in Azure Create an ASP.NET Core web app in Azure

  2. Go to your web app's Configuration page.

AppConfiguration

Add your Azure Storage's connection string AzureStorage.

AddConnectionString

Similarly, add your Azure SignalR's connection string as Azure__SignalR__ConnectionString. Then restart your web app.

Congratulations! Your chat room web app is running live in Azure App Service.