Constant state of connection identifier through browser session using signalR

I am trying to create a chat engine in which the user ID is represented by their automatically generated connection identifier signalR. When the page is refreshed, the connection identifier changes when a new connection is created. Is there a way to save the state of the user connection identifier until the end of the browser session (i.e. until it ends the session on the client).

Any guidance or documentation? That would really help.

+4
source share
1 answer

The most common way would be to maintain a static List<CustomType> in your hub, where CustomType has the UserId and ConnectionId properties

Here is an example you can follow: http://www.tugberkugurlu.com/archive/mapping-asp-net-signalr-connections-to-real-application-users

I write my connection identifiers to the sql database in the SignalR.OnConnected event and delete them in the SignalR.OnDisconnected event, you will get some records of lost connections in your table that are not very important, just make sure you have the created_date column and ignore any connections that take so many hours or days depending on your needs. There is also no session in SignalR, don’t even try, this is a known issue. To get your current user, modern out-of-the-box Microsoft MVC membership providers work fine. But you can also easily use cookies to get the current username / id.

+4
source

Source: https://habr.com/ru/post/1495465/


All Articles