How to use SignalR in an Azure function app?

▼魔方 西西 提交于 2021-01-20 16:54:18

问题


I'm building a small game that will be driven by web socket using SignalR on one hand and Azure function app on the other hand. Basically, the user establishes a web socket connection with the server and sends/receives a message from it. This is mainly done this way because players can discuss with each other in real-time.

Besides, I'd like to have some Azure function apps that run and execute some instructions. For example, every minute, an app would make some monsters move. If these monsters are around a specific player, I'd like him to know.

For this, I have two solutions in mind:

  • Requesting the information every second from the client and then warning the user if he needs to be.
  • Opening a connection to my web socket from within my function app to send the data and the hub would forward the information to affected users.

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

The second option seems better but as I'm not familiar with function apps yet, I'm wondering if it's the way to go. Is it possible/correct to open a web socket connection from a function app?

Maybe there are some better options?


回答1:


For example, every minute, an app would make some monsters move. If these monsters are around a specific player, I'd like him to know.

If you’d like to call hub method from your Azure Functions app to broadcast monsters position info to specific players, you can refer to the following sample that 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"
      }
    }
   }
}

Client user can receive message that Azure Functions app send

Besides, if you want to broadcast to specific players instead of all connecting players, you can refer to the following code.

Clients.Clients(clientids_list).addNewMessageToPage(MonsterPositionInfo);


来源:https://stackoverflow.com/questions/45554589/how-to-use-signalr-in-an-azure-function-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!