如何使 .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 服务还很陌生,我需要帮助提前谢谢你

Harshitha Veeramalla-MT

SignalR 服务配置

可以在不同模式下配置 Azure SignalR 服务。与 Azure Functions 一起使用时,必须在无服务器模式下配置服务转到 Azure 门户,找到SignalR服务资源“设置”页面服务模式设置Serverless

在此处输入图片说明

Azure 函数开发

使用 Azure Functions 和 Azure SignalR 服务构建的无服务器应用程序需要两个 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 服务,客户端必须完成成功的连接

  1. 协商HTTP 端点发出请求以获取有效的连接信息
  2. 使用从协商端点获取的服务端点 URL 和访问令牌连接到 SignalR 服务

从客户端向服务发送消息

 connection.send('method1', 'arg1', 'arg2');

Azure 函数配置

与 Azure SignalR 服务集成的 Azure Function 应用程序可以像任何典型的 Azure Functions 应用程序一样使用持续部署zip 部署从包运行等技术进行部署

启用 CORS

本地主机

云 - Azure 函数 CORS

要在 Azure 函数应用程序上启用 CORS,请转到 Azure 门户 --> 函数应用程序 --> 平台功能 --> CORS 配置 在此处输入图片说明

有关更多信息,请遵循MS 文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章