首页 > 解决方案 > 如何将消息从 js 库发送到 Azure SignalR Serverless 中的组

问题描述

您好我正在尝试使用 Azure Signal R Serverless JS Client Js Library向组发送消息。

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

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

*其中 signalRMessages = IAsyncCollector signalRMessages

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

标签: signalrserverlessasp.net-core-signalrsignalr-client

解决方案


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

您可以参考这个 github 存储库,其中显示了如何使用 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:从这里,我们可以找到:

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


推荐阅读