.net-core - 如何使 .Net Core Console 应用程序连接到 SignalR 服务(在无服务器模式下)然后从服务器接收消息
问题描述
所以我写了一个 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 服务还很陌生,我需要帮助提前谢谢你
解决方案
SignalR 服务配置
Azure SignalR 服务可以配置为不同的模式。与 Azure Functions 一起使用时,必须在无服务器模式下配置服务。转到 Azure 门户,找到SignalR服务资源的设置页面。将服务模式设置为无服务器。
Azure 函数开发
使用 Azure Functions 和 Azure SignalR Service 构建的无服务器应用程序需要两个 Azure Functions:
- 客户端调用以获取有效 SignalR 服务访问令牌和服务端点 URL 的“协商”函数。
- 处理来自 SignalR 服务的消息并发送消息或管理组成员资格的函数。
处理从 SignalR 服务发送的消息
使用SignalR 触发器绑定来处理从 SignalR 服务发送的消息。有关详细信息,请参阅SignalR 触发器 绑定参考。
发送消息和管理组成员身份
使用SignalR输出绑定将消息发送到连接到 Azure SignalR 服务的客户端。请参阅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 服务,客户端必须完成成功连接
- 向协商HTTP 端点发出请求以 获取有效的连接信息
- 使用服务端点 URL 和从 协商 端点获取的访问令牌连接到 SignalR 服务
从客户端向服务发送消息
connection.send('method1', 'arg1', 'arg2');
Azure 函数配置
与 Azure SignalR 服务集成的 Azure Function 应用可以像任何典型的 Azure Function 应用一样部署,使用持续部署、zip 部署和从包运行等技术。
启用 CORS
本地主机
云 - Azure Functions CORS
要在 Azure 函数应用上启用 CORS,请转到 Azure 门户 --> 函数应用 --> 平台功能 --> CORS 配置
有关更多信息,请关注MS 文档
推荐阅读
- css - Bootstrap 4 - 将行向上移动
- android - RatingBar 更改星级值评级值
- maven - groovy-maven-plugin:2.0:execute failed: No such property: canonicalFile for class: java.io.File
- scala - 使用 web-app 进行分布式计算的 Akka 架构
- c# - 创建另一个类的后代
- r - 皮尔逊相关系数与 r 中的两个表
- python - 如何遍历下拉模型表单字段以仅显示 Django 中的某些值
- c# - 具有相互引用的泛型类型的 C# 接口
- coq - 导入后无法使用 coq 的 NDiv
- java - 无法调用对象的方法