6

I have the following problem with SignalR with users accessing my website from a mobile device. The problem is that when the phone locks or the user goes to another app, the Disconnect() method is fired on the server (I try sending a message to the server and waiting for it to reply trough SignalR but there is no reply from the server after the Disconnect method is fired on server). After this it seams as the client is no more connected to the server.

How can the client tell the server that it is back?

Ryan
  • 5,456
  • 25
  • 71
  • 129

3 Answers3

5

You can stop the connection:

$.connection.hub.stop() 

and then restart it via

$.connection.hub.start();

In the next version of SignalR we will take care of this for you, but for the interim you will have to manage it yourself. I'd recommend detecting on the client if the server is dead and then running the stop -> start.

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • I still see this in some cases, even today on the current version - so a hint: make sure you do any "start successful" activities on each start again if appropriate. – MikeBaz - MSFT Sep 07 '14 at 18:47
4

We had a similar situation connecting from mobile devices. In our case, calling .Stop() or trying to dispose the existing connection took 30 seconds, depending how long the underlying connection was inactive. (the underlying protocol has a timeout you can configure)

Our solution was to clean up any handled events and dereference the hub connection object - and then just start fresh with a new instance after the app is reactivated. This works reliably and is almost instant. The server disconnect event fires correctly as well, although it is fired for each instance that disconnects.

I've documented our journey and solution in this post Best practice for reconnecting SignalR 2.0 .NET client to server hub. Hope it is helpful.

Community
  • 1
  • 1
Ender2050
  • 6,912
  • 12
  • 51
  • 55
0

I think Disconnect on the server triggers the client subscriptions to be removed from memory.

Therefore, when the client comes back, the server has no knowledge of the client.

Will
  • 773
  • 1
  • 7
  • 24