how to get SignalR user connection id out side the

2019-04-17 20:20发布

I am using SignalR in my ASP.NET web application. Here I am calling client from outside to hub class using IHubContext. I need to get the current user's connection ID in order to send messages to the current user only. How can I get the connection ID on the client side?

7条回答
不美不萌又怎样
2楼-- · 2019-04-17 21:02

There's another way also, you can get connection id into your controller from hub by invoking a method of hub and you can return the required ID from there.

Controller Code

var HubContext = GlobalHost.ConnectionManager.GetHubContext<"ChatHub">(); //`ChatHub` can be your Hub Name
ChatHub HubObj= new ChatHub();
var RequiredId= HubObj.InvokeHubMethod();

Code inside Hub

public string InvokeHubMethod()
{
     return "ConnectionID"  //ConnectionID will the Id as string that you want outside the hub
}
查看更多
登录 后发表回答