所以我写了一个 Azure 函数如下:
public static class Negotiate
{
[FunctionName("Negotiate")]
public static SignalRConnectionInfo GetSignalRInfo(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
[SignalRConnectionInfo(HubName = "chat")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
}
[FunctionName("messages")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] object message,
[SignalR(HubName = "chat")] IAsyncCollector<SignalRMessage> signalRMessages)
{
/* it passes in SignalRMessage that consists of two things:
Target : this is the name of an event, in this case, newMessage. A client can listen to this event and render its payload for example
Arguments : this is simply the payload, in this case, we just want to broadcast all messages that come from one client,
to ensure other listening clients would be updated on the fact that there is new data.*/
return signalRMessages.AddAsync(
new SignalRMessage
{
Target = "newMessage",
Arguments = new[] { message }
});
}
}
现在我想编写一个 .Net Core 控制台应用程序,以便能够连接到 Azure SignalR 服务并从 AzureFunction 接收消息我对 Azure 服务还很陌生,我需要帮助提前谢谢你
可以在不同模式下配置 Azure SignalR 服务。与 Azure Functions 一起使用时,必须在无服务器模式下配置服务。转到 Azure 门户,找到SignalR服务资源的“设置”页面。将服务模式设置为Serverless。
使用 Azure Functions 和 Azure SignalR 服务构建的无服务器应用程序需要两个 Azure Functions:
使用SignalR 触发器绑定来处理从 SignalR 服务发送的消息。有关详细信息,请参阅SignalR 触发器绑定参考。
使用SignalR输出绑定向连接到 Azure SignalR 服务的客户端发送消息。请参阅SignalR输出绑定参考。
public class SignalRTestHub : ServerlessHub
{
[FunctionName("negotiate")]
public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req)
{
return Negotiate(req.Headers["x-ms-signalr-user-id"], GetClaims(req.Headers["Authorization"]));
}
[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger]InvocationContext invocationContext, ILogger logger)
{
await Clients.All.SendAsync(NewConnectionTarget, new NewConnection(invocationContext.ConnectionId));
logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}
[FunctionName(nameof(Broadcast))]
public async Task Broadcast([SignalRTrigger]InvocationContext invocationContext, string message, ILogger logger)
{
await Clients.All.SendAsync(NewMessageTarget, new NewMessage(invocationContext, message));
logger.LogInformation($"{invocationContext.ConnectionId} broadcast {message}");
}
[FunctionName(nameof(OnDisconnected))]
public void OnDisconnected([SignalRTrigger]InvocationContext invocationContext)
{
}
}
使用SignalRFilterAttribute
客户端开发 配置客户端连接要连接到 SignalR 服务,客户端必须完成成功的连接
connection.send('method1', 'arg1', 'arg2');
与 Azure SignalR 服务集成的 Azure Function 应用程序可以像任何典型的 Azure Functions 应用程序一样使用持续部署、zip 部署和从包运行等技术进行部署。
云 - Azure 函数 CORS
要在 Azure 函数应用程序上启用 CORS,请转到 Azure 门户 --> 函数应用程序 --> 平台功能 --> CORS 配置
有关更多信息,请遵循MS 文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句