如何在 Azure 函数应用程序中使用 SignalR?
How to use SignalR in an Azure function app?
我正在构建一个小游戏,该游戏将由网络套接字驱动,一方面使用 SignalR,另一方面使用 Azure 函数应用程序。基本上,用户与服务器建立一个 web 套接字连接,并 sends/receives 来自它的消息。这样做主要是因为玩家可以实时互相讨论。
此外,我想要一些 Azure 函数应用程序 运行 并执行一些指令。例如,每分钟,一个应用程序都会让一些怪物移动。如果这些怪物在特定玩家周围,我想让他知道。
为此,我想到了两个解决方案:
- 每秒从客户端请求信息,然后在需要时警告用户。
- 从我的函数应用程序中打开到我的网络套接字的连接以发送数据,集线器会将信息转发给受影响的用户。
第一个选项对我来说有点破坏了网络套接字的目的。如果我需要轮询服务器以获取某些信息,那么使用 Web 套接字有什么意义。
第二个选项似乎更好,但由于我还不熟悉函数应用程序,所以我想知道是否可以这样做。从函数应用程序打开网络套接字连接是possible/correct吗?
也许有更好的选择?
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 方法来向特定玩家广播怪物位置信息,你可以参考下面的示例,我这边效果很好。
中心 class
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 应用程序发送的消息
此外,如果你想广播给特定的玩家而不是所有连接的玩家,你可以参考下面的代码。
Clients.Clients(clientids_list).addNewMessageToPage(MonsterPositionInfo);
我正在构建一个小游戏,该游戏将由网络套接字驱动,一方面使用 SignalR,另一方面使用 Azure 函数应用程序。基本上,用户与服务器建立一个 web 套接字连接,并 sends/receives 来自它的消息。这样做主要是因为玩家可以实时互相讨论。
此外,我想要一些 Azure 函数应用程序 运行 并执行一些指令。例如,每分钟,一个应用程序都会让一些怪物移动。如果这些怪物在特定玩家周围,我想让他知道。
为此,我想到了两个解决方案:
- 每秒从客户端请求信息,然后在需要时警告用户。
- 从我的函数应用程序中打开到我的网络套接字的连接以发送数据,集线器会将信息转发给受影响的用户。
第一个选项对我来说有点破坏了网络套接字的目的。如果我需要轮询服务器以获取某些信息,那么使用 Web 套接字有什么意义。
第二个选项似乎更好,但由于我还不熟悉函数应用程序,所以我想知道是否可以这样做。从函数应用程序打开网络套接字连接是possible/correct吗?
也许有更好的选择?
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 方法来向特定玩家广播怪物位置信息,你可以参考下面的示例,我这边效果很好。
中心 class
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 应用程序发送的消息
此外,如果你想广播给特定的玩家而不是所有连接的玩家,你可以参考下面的代码。
Clients.Clients(clientids_list).addNewMessageToPage(MonsterPositionInfo);