How to use SignalR in an Azure function app?

I am creating a small game that will be controlled through a web socket using SignalR, on the one hand, and an Azure function application, on the other. In fact, the user establishes a connection from the web socket to the server and sends / receives a message from him. This is mainly done because players can communicate with each other in real time.

In addition, I would like to have several Azure feature applications that start and follow some instructions. For example, every minute the application makes some monsters move. If these monsters are around a specific player, I would like him to know.

For this, I have two solutions:

  • Request information every second from the client, and then alert the user if he should.
  • By opening a connection to my web socket from my functional application to send data, the hub will redirect the information to the affected users.

The first option is to kind of defeat the purpose of the web socket for me. What is the point of having a web socket if I need to poll the server for any information.

The second option seems better, but since I am not familiar with functional applications, I wonder if it is worth going further. Is it possible / to open a connection through a web socket correctly from a functional application?

Maybe there are better options?

+8
source share
1 answer

For example, every minute the application makes some monsters move. If these monsters are around a certain player, I would like him to know.

If you like to call the hub method from your Azure Functions application to transfer information about the location of monsters to certain players, you can refer to the following sample, which works fine on my side.

Hub class

public class ChatHub : Hub { public void BroadcastMonstersPosition(string MonsterPositionInfo) { Clients.All.addNewMessageToPage(MonsterPositionInfo); } //other hub methods } 

Azure Functions app (timerTrigger)

 using System; public static void Run(TimerInfo myTimer, TraceWriter log) { var hub = new Microsoft.AspNet.SignalR.Client.HubConnection("http://xxxxxx.azurewebsites.net/signalr/hubs"); var proxy = hub.CreateHubProxy("ChatHub"); hub.Start().Wait(); //invoke hub method proxy.Invoke("BroadcastMonstersPosition", "new position info"); log.Info($"C# Timer trigger function executed at: {DateTime.Now}"); } 

function.json

 { "bindings": [ { "name": "myTimer", "type": "timerTrigger", "direction": "in", "schedule": "0 */1 * * * *" } ], "disabled": false } 

project.json

 { "frameworks": { "net46":{ "dependencies": { "Microsoft.AspNet.SignalR.Client": "2.2.0" } } } } 

A customer user may receive a message that the Azure Functions application is sending

enter image description here

In addition, if you want to broadcast specific players, and not all connecting players, you can refer to the following code.

 Clients.Clients(clientids_list).addNewMessageToPage(MonsterPositionInfo); 
+18
source

Source: https://habr.com/ru/post/1270657/


All Articles