SignalR - how to connect the client to the server (how to restart SignalR)

I have the following problem with SignalR when users access my site from a mobile device. The problem is that when the phone is blocked or the user switches to another application, the Disconnect () method starts on the server (I try to send a message to the server and wait for it to respond through SignalR, but there is no response from it after the Disconnect method is launched on server). After that, it drops, because the client is no longer connected to the server.

How can a client tell the server that it has returned?

+4
source share
3 answers

You can stop the connection:

$.connection.hub.stop() 

and then restart it with

 $.connection.hub.start(); 

In the next version of SignalR, we will take care of this for you, but in the interim you will have to manage it yourself. I would recommend detecting on the client if the server is dead, and then starting stop → start.

+5
source

We had a similar situation with mobile devices. In our case, calling .Stop () or trying to delete an existing connection took 30 seconds, depending on how long the base connection was inactive. (the base protocol has a timeout that you can configure)

Our solution was to clear any processed events and dereference the hub connection object, and then simply start a new one with a new instance after the application resumes. It works reliably and almost instantly. The server shutdown event also fires correctly, although it fires for each individual instance that disconnects.

I documented our journey and solution in this post. Best practice for reconnecting the SignalR 2.0.NET client to the server node . Hope this will be helpful.

+4
source

I think that Disconnect on the server launches client subscriptions that need to be removed from memory.

Therefore, when the client returns, the server does not know about the client.

0
source

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


All Articles