例如,每分钟,一个应用程序都会让一些怪物移动。如果这些怪物在某个特定玩家周围,我希望他知道。
如果你想从你的 Azure Functions 应用中调用 hub 方法来向特定玩家广播怪物的位置信息,你可以参考以下在我这边工作正常的示例。
中心类
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);