如何在 Azure 函数应用中使用 SignalR? [英] How to use SignalR in an Azure function app?

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

问题描述

我正在构建一个由 Web 套接字驱动的小游戏,一方面使用 SignalR,另一方面使用 Azure 函数应用程序.基本上,用户与服务器建立 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 套接字有什么意义.

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?

也许有更好的选择?

推荐答案

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

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 应用中调用 hub 方法来向特定玩家广播怪物的位置信息,你可以参考下面这个在我这边工作正常的示例.

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天全站免登陆