How do I get the clients connectionId/clientId outside of the Hub?.. I have managed to do the following:
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
But in that context-object there is no such thing as a clientId
.
You can invoke a method inside the hub and you can return the connection ID from there. You'll get required ID outside the hub in connectedUserID Variable. Hope This Helps.
Each client connecting to a hub passes a unique connection id. You can retrieve this value in the Context. ConnectionId property of the hub context.
What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.
You could implement IConnected/IDisconnect on the Hub and manually keep track of clients for example in a database, then pull back the list when required. The example below is from the SignalR Wiki
public class Status : Hub, IDisconnect, IConnected
{
public Task Disconnect()
{
return Clients.leave(Context.ConnectionId, DateTime.Now.ToString());
}
public Task Connect()
{
return Clients.joined(Context.ConnectionId, DateTime.Now.ToString());
}
public Task Reconnect(IEnumerable<string> groups)
{
return Clients.rejoined(Context.ConnectionId, DateTime.Now.ToString());
}
}
Why would there be a connection Id on the global context? Which connection would it be in reference to? When you get the global context, you are accessing a one way channel from server to client and can send messages over it. You don't have access to the connection id of the hub since you aren't calling into it. You can store them somewhere in your application if you need to use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With