Server side:
public override Task OnConnected()
{
var connectionId = Context.ConnectionId;
var user = Context.User.Identity.Name; // Context.User is NULL
return base.OnConnected();
}
Client side (in Console project):
IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();
When the client connect to the server, I want to know the map between userName and connectionId, But Context.User
is NULL. How do I set this value in the client side?
Single-user groups You can create a group for each user, and then send a message to that group when you want to reach only that user. The name of each group is the name of the user. If a user has more than one connection, each connection id is added to the user's group.
The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.
IIS on client operating systems has a limit of 10 concurrent connections. SignalR's connections are: Transient and frequently re-established. Not disposed immediately when no longer used.
try this with queryString in asp.netcore 2.1:
Client (javascript) set query string after url like follow:
var connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:10499/chathub?username=xxxx").build();
connection.start().then(function ()
{
// do some thing here ...
}).catch(function (err)
{
console.error(err.toString());
});
.
.
.
Server
public override Task OnConnectedAsync()
{
var username = Context.GetHttpContext().Request.Query["username"];
// username = xxxx
return base.OnConnectedAsync();
}
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