从控制器调用 SignalR Core Hub 方法时的连接 ID [英] Connection ID when calling SignalR Core Hub method from Controller

查看:22
本文介绍了从控制器调用 SignalR Core Hub 方法时的连接 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对另一个问答的跟进.从控制器调用 HubContext.Clients.CallerHubContext.Clients.Others 有什么影响?我看到这取决于 身份证.在这种情况下它会有什么价值?

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

解决方案

从控制器调用 HubContext.Clients.CallerHubContext.Clients.Others 有什么影响?我看到这取决于连接 ID.在这种情况下它会有什么价值?

HubContext.Clients 上既没有 .Caller 也没有 .Others(HubClients<>/p>

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

<块引用>

(从控制器操作中)我如何获得可以与 HubContext.Clients 的方法一起使用的连接 ID(对于当前调用 Web API 的客户端)?

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

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

"如果您有权访问发起操作的用户的用户 ID,则可以使用 .Users(userId) 向该用户的所有连接发送消息.类似地,您将 SignalR 连接添加到一个组并使用 .Group(groupName)"
aspnet/SignalR#2274(评论)

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

您可以在调用 API 的客户端上获取连接 ID,然后将其发送给控制器.

中心:

public string GetConnectionId(){返回 Context.ConnectionId;}

客户:

hub.invoke('getConnectionId').then(function (connectionId) {//将 connectionId 发送到控制器});

This is a follow-up to another question and answer. 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?

If the connection ID (and thus Caller and Others) is invalid then (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?

解决方案

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?

There is neither .Caller nor .Others on HubContext.Clients (of type HubClients<THub>).

"It's not possible to access those from IHubContext. Caller and Others both depend on knowing which client is the caller. When you're in IHubContext, we don't know and there may not even be a current caller since you could be in an MVC controller action, etc."
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?

"there is no way of knowing who the current caller is from the IHubContext"
aspnet/SignalR#2274 (comment)

Using Groups and Users is one way to mitigate this.

"If you have access to the User ID of the user who initiated the action, you can use .Users(userId) to send a message to all of that user's connections. Similarly, you add the SignalR connections to a group and send to that group using .Group(groupName)"
aspnet/SignalR#2274 (comment)

Alternatively, get the connection ID on the client side.

You can get the connection ID on the client that calls the API, and then send it to the controller.

Hub:

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

Client:

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

这篇关于从控制器调用 SignalR Core Hub 方法时的连接 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆