OnDisconnect and Logged on Users after recycle?

2019-08-08 18:28发布

I've read that OnDisconnect ( server side) sometimes , can be bypassed ( application recycle etc).

The OnDisconnected method doesn't get called in some scenarios, such as when a server goes down or the App Domain gets recycled.

And so I ask :

When a user connects , I generate him a token , keep it in the user cookie , and when OnConnect is called , I attach a connectionId with that token.

(the same way where onDiconnect is called , I set a value in the dateDisconnected)

This will also supports 2 connected users ( same browser)

example :

enter image description here

example :

id         tokenId              ConnectionID         DateCreated            dateDisconnected
----------------------------------------------------------------------------------------------
1          500                    {1234-1234}          06-07-2013               null             
2          500                    {5432-5432}          06-07-2013               null             

And here is the problem :

What will happen if a user has logged in (So I write this user in my DB as logged in) , do some chat , and then the server restarted ?

The client will try to reconnect and after 30 seconds it will stop.

But then , when the server is UP (after an hour ) - I STILL HAVE A LOGGED ON USER IN MY DB. and he is NOT. ( well , not with that connection ID anyway...)

How can I solve this kind of problem ?

标签: signalr
1条回答
太酷不给撩
2楼-- · 2019-08-08 18:51

The best way to solve this problem is to log everyone out on application start, aka in your case invalidate/delete all logged in token rows. Therefore whenever the server comes up the application is in a fresh state with no one logged in.

查看更多
登录 后发表回答