如果客户端仍连接到 SignalR 服务,请签入 Azure Functions

Check in Azure Functions if client is still connected to SignalR Service

我在 Azure Functions 中创建了协商和发送消息函数(类似于下面的示例)以合并 SignalR 服务。我正在使用自定义身份验证机制在 SignalRMessage 上设置 UserId

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service?tabs=csharp

[FunctionName("negotiate")]
public static SignalRConnectionInfo Negotiate(
    [HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req, 
    [SignalRConnectionInfo
        (HubName = "chat", UserId = "{headers.x-ms-client-principal-id}")]
        SignalRConnectionInfo connectionInfo)
{
    // connectionInfo contains an access key token with a name identifier claim set to the authenticated user
    return connectionInfo;
}

[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 this user ID
            UserId = "userId1",
            Target = "newMessage",
            Arguments = new [] { message }
        });
}

如果客户端不再连接,我想发送推送通知,而不是向 IAsyncCollector 添加新对象。我还正确设置了 AppCenter 推送框架,但我遇到了问题。有没有一种简单的方法可以找出哪个 UserId 仍然连接到集线器?这样,我可以决定发送推送。关于此问题的推荐 Microsoft 指南是什么?

查看此功能:Azure SignalR Service introduces Event Grid integration feature,其中 SignalR 服务 发出以下两种事件类型:

Microsoft.SignalRService.ClientConnectionConnected

Microsoft.SignalRService.ClientConnectionDisconnected

更多详情here