从 Controller 调用 SignalR Core Hub 方法时的连接 ID

Connection ID when calling SignalR Core Hub method from Controller

这是 . What's the effect of calling HubContext.Clients.Caller or HubContext.Clients.Others from the controller? I see it depends on the connection ID 的后续。在这种情况下它有什么价值?

如果连接 ID(因此 CallerOthers)无效,那么(从控制器操作中)我如何获得连接 ID(对于当前调用 Web 的客户端 API) 我可以用 HubContext.Clients 的方法吗?

What's the effect of calling HubContext.Clients.Caller or HubContext.Clients.Others from the controller? I see it depends on the connection ID. What value would it have in this situation?

HubContext.Clients 上既没有 .Caller 也没有 .OthersHubClients<THub> 类型)。

"不可能从 IHubContext 访问那些。CallerOthers 都取决于知道哪个客户端是调用者。当你在IHubContext,我们不知道,甚至可能 不是 当前调用者,因为您可能处于 MVC 控制器操作等 "
aspnet/SignalR#2274 (comment)

(from within the controller action) how could I obtain a connection ID (for the client currently calling the Web API) that I could use with HubContext.Clients's methods?

"无法从 IHubContext 知道当前调用者是谁"
aspnet/SignalR#2274 (comment)

使用组和用户是缓解这种情况的一种方法。

"如果您有权访问发起操作的用户的用户 ID,则可以使用 .Users(userId) 向该用户的所有联系人发送消息。同样,您添加SignalR 连接到一个组并使用 .Group(groupName)"
发送到该组 — aspnet/SignalR#2274 (comment)

或者,在客户端获取连接 ID。

可以在调用API的客户端获取连接ID,然后发送给controller。

中心:

public string GetConnectionId()
{
    return Context.ConnectionId;
}

客户:

hub.invoke('getConnectionId')
    .then(function (connectionId) {
        // Send the connectionId to controller
    });