如何在Azure功能应用程序中使用SignalR? [英] How to use SignalR in an Azure function app?

查看:53
本文介绍了如何在Azure功能应用程序中使用SignalR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小型游戏,该游戏将由一方面使用SignalR并由Azure功能应用程序通过Web套接字驱动.基本上,用户与服务器建立Web套接字连接,并从服务器发送/接收消息.这主要是因为玩家可以实时进行讨论.

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.

此外,我想拥有一些运行并执行一些指令的Azure功能应用程序.例如,每分钟,应用程序都会使一些怪物移动.如果这些怪物在特定玩家周围,我希望他知道.

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:

  • 每秒从客户端请求信息,然后警告用户是否需要.
  • 从功能应用程序中打开与Web套接字的连接以发送数据,然后集线器会将信息转发给受影响的用户.

第一种选择对我来说是一个Web套接字的目标.如果需要轮询服务器以获取一些信息,那么使用Web套接字有什么意义.

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.

第二个选项似乎更好,但是由于我还不熟悉功能应用程序,所以我想知道这是否可行.从功能应用程序打开Web套接字连接是否可能/正确?

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?

推荐答案

例如,每分钟,应用程序都会使一些怪物移动.如果这些怪物在特定玩家周围,我希望他知道.

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.

如果您想从Azure Functions应用中调用集线器方法以将怪物的位置信息广播给特定玩家,则可以参考以下对我而言效果很好的示例.

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.

集线器

public class ChatHub : Hub
{       

    public void BroadcastMonstersPosition(string MonsterPositionInfo)
    {
        Clients.All.addNewMessageToPage(MonsterPositionInfo);
    }

    //other hub methods
}  

Azure Functions应用(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"
      }
    }
   }
}

客户端用户可以接收Azure Functions应用发送的消息

此外,如果要广播给特定的播放器而不是所有连接的播放器,则可以参考以下代码.

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);

这篇关于如何在Azure功能应用程序中使用SignalR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆