【问题标题】:How to send message from the js library to a group in Azure SignalR Serverless如何将消息从 js 库发送到 Azure SignalR Serverless 中的组
【发布时间】:2020-12-03 23:05:06
【问题描述】:

您好,我正在尝试使用 Azure Signal R 无服务器 JS 客户端 Js Library 向组发送消息。

我可以通过 Azure 无服务器函数执行此操作,如下所示:

await signalRMessages.AddAsync(
                new SignalRMessage
                {
                    GroupName = m.GroupName,
                    Target = m.Target,
                    Arguments = new[] { m.Message }
                });

*其中 signalRMessages = IAsyncCollector signalRMessages

如何从 js 库发送同样的消息?

【问题讨论】:

    标签: signalr serverless asp.net-core-signalr signalr-client


    【解决方案1】:

    尝试使用 Azure Signal R Serverless 向组发送消息

    您可以参考this github repo,它通过示例代码展示了如何使用 Azure SignalR 服务在 Azure 函数中实现组广播功能。

    使用 SignalRGroupAction 类将用户添加到组

    return signalRGroupActions.AddAsync(
        new SignalRGroupAction
        {
            ConnectionId = decodedfConnectionId,
            UserId = message.Recipient,
            GroupName = message.Groupname,
            Action = GroupAction.Add
        });
    

    在客户端,向端点发出请求以将用户添加到组中

    function addGroup(sender, recipient, connectionId, groupName) {
            return axios.post(`${apiBaseUrl}/api/addToGroup`, {
              connectionId: connectionId,
              recipient: recipient,
              groupname: groupName
            }, getAxiosConfig()).then(resp => {
              if (resp.status == 200) {
                confirm("Add Successfully")
              }
            });
          }
    

    测试结果

    更新:

    问:“从 JS 客户端直接从套接字发送消息”。

    A:从here,我们可以找到:

    尽管 SignalR SDK 允许客户端应用程序调用 SignalR 集线器中的后端逻辑,但当您将 SignalR 服务与 Azure Functions 一起使用时,尚不支持此功能。使用 HTTP 请求调用 Azure Functions。

    【讨论】:

    • 谢谢!您在此处发布的答案是如何加入群组......我完成了所有工作以及向群组发送消息(通过 HTTP 触发器)我想要做的是从 JS 客户端发送消息(直接来自插座)
    • The answer you posted here is how to join a group 如果你查看我分享的 github repo,你会发现完整的示例代码。而function sendMessage 可以帮助向特定群组发送消息。
    • What I want to do is send the message from the JS Client (straight from the socket) 来自here,您可以找到:"虽然 SignalR SDK 允许客户端应用程序调用 SignalR 集线器中的后端逻辑,但在您使用 SignalR 时尚不支持此功能使用 Azure Functions 提供服务。使用 HTTP 请求调用 Azure Functions。"
    • 谢谢,好像还不支持。我使用 HTTP 请求发送没有问题...如果您使用此信息更新答案,我将标记为正确答案。
    【解决方案2】:

    现在看来这是可能的......

    https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-concept-serverless-development-config#sending-messages-from-a-client-to-the-service

    从客户端发送消息到服务如果你有上游 为您的 SignalR 资源配置,您可以从 使用任何 SignalR 客户端连接到 Azure Functions。这是一个 JavaScript 中的示例:

    JavaScript

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多