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

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

问题描述

这是另一个问题和答案的后续内容.从控制器调用HubContext.Clients.CallerHubContext.Clients.Others有什么作用?我看到它取决于连接ID .在这种情况下它将具有什么价值?

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?

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

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?

推荐答案

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

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也没有.Others(类型为

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

"无法从IHubContext访问它们.CallerOthers都取决于知道哪个客户端是调用方.当您使用IHubContext时,我们不知道并且可能甚至成为当前呼叫者,因为您可能正在执行MVC控制器操作等."
aspnet/SignalR#2274(评论)

"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)

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

(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(评论)

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

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

"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)

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

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

集线器:

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

客户:

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

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

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