Reputation: 5321
I have created a SignalR hub endpoint in .NET 8. The client (JavaScript or Angular) successfully establishes a connection when connecting to the SignalR hub endpoint.
When I connect to the https://
SignalR hub endpoint, the connection ID has a valid value.
However, when connecting to the wss://
SignalR hub endpoint, the connection ID is NULL
. According to this documentation, this is the expected behavior.
My concern is how to retrieve the connection ID after the connection is established.
Upvotes: 0
Views: 37
Reputation: 21883
We can get connectionId from client and server side. The document you shared is from Microsoft.AspNetCore.SignalR.Client
.
Now I am try to connect to signalr connection by using wss://
in websocket test tool. And get connectionId from server side. The null issue you found is on client side.
Here is the test code and test result.
public override async Task OnConnectedAsync()
{
StringBuilder strSql = new StringBuilder();
// Get HttpContext In asp.net core signalr
//IHttpContextFeature? hcf = this.Context?.Features?[typeof(IHttpContextFeature)] as IHttpContextFeature;
//HttpContext? hc = hcf?.HttpContext;
// userid
string? uid = Context?.GetHttpContext()?.Request.Query["uid"].ToString();
//string? uid = hc?.Request?.Path.Value?.Split(new string[] { "=", "" }, StringSplitOptions.RemoveEmptyEntries)[1].ToString();
// system id
string? sid = Context.GetHttpContext()?.Request.Query["sid"].ToString();
//string? sid = hc?.Request?.Path.Value?.Split(new string[] { "=", "" }, StringSplitOptions.RemoveEmptyEntries)[2].ToString();
string? userid = uid;
if (userid == null || userid.Equals(string.Empty))
{
Trace.TraceInformation("userid is required, can't connect signalr service");
return;
}
Trace.TraceInformation(userid + "connected");
// save connection
List<string>? existUserConnectionIds;
ConnectedUsers.TryGetValue(userid, out existUserConnectionIds);
if (existUserConnectionIds == null)
{
existUserConnectionIds = new List<string>();
}
existUserConnectionIds.Add(Context!.ConnectionId);
ConnectedUsers.TryAdd(userid, existUserConnectionIds);
// For testing
Clients.All.SendAsync(Context.ConnectionId);
await base.OnConnectedAsync();
}
Upvotes: 0