首页 > 解决方案 > 在 Azure Functions 的 SignalR 服务中向一组用户发送消息

问题描述

查看 SignalR 绑定的文档以向指定用户发送消息,您UserId在消息中包含该属性 -

[FunctionName("SendMessage")]
public static Task SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message, 
    [SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
    return signalRMessages.AddAsync(
        new SignalRMessage 
        {
            // the message will only be sent to these user IDs
            UserId = "userId1",
            Target = "newMessage", 
            Arguments = new [] { message } 
        });
}

此示例直接取自文档,但注释暗示您向多个用户 ID 发送消息,即使该属性是字符串而不是数组。

您将如何指定多个用户?(例如,如果他们一起在私人聊天频道中)或者是评论措辞中的错误,您需要为每个用户发送消息?

对于其他版本的 SignalR,我会将它们放在一个组中,但是函数不存在对此的绑定。

标签: azureazure-functionssignalr-service

解决方案


最新版本中引入了组操作。

现在你可以:

  • GroupName使用in向群组发送消息SignalRMessage
  • IAsyncCollector<SignalRGroupAction>使用输出在组中添加/删除用户

推荐阅读