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:
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?
SignalR Service transmits data from the client to the pairing application server. And data from the application server will be sent to the mapped clients. SignalR Service does not save or store customer data, all customer data received is transmitted to target server or clients in real-time.
Azure SignalR Service provides REST API to support server to client communication scenarios, such as broadcasting. You can choose any programming language that can make REST API call. You can post messages to all connected clients, a specific client by name, or a group of clients.
Enable App Service Authentication Select the subscription and function app name to open the function app in the Azure portal. In the function app that was opened in the portal, locate the Platform features tab, select Authentication/Authorization. Turn On App Service Authentication.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With