2

I am setting my GlobalHost Configuration like this by following this answer to listen when the client is unreachable:

 GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(50);
 GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
 GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

And Overriding the OnDisconnected method in my HUB Class to set the client has been disconnected

 public override Task OnDisconnected(bool stopCalled) {
       /*My code for saving the information of disconnecting*/
        return base.OnDisconnected(stopCalled);      
    }

I am using Xamarin for android as a client and calling the Stop() method by overriding the OnStop() method of my activity, like this:

       protected override void OnStop()
        {

         //hubConnection.Stop(); previously I was using this but it takes too long to stop the hub connection in this way. so I wrote an explicit method and invoke it .

           Task hubconnection = serverHub.Invoke("StopConnection", new object[] { MethodToIdentifyDevice() }).ContinueWith(r =>
            {

            });


            base.OnStop();
        }

Secondly, I have written an explicit hubmethod to invoke when to notify my server explicitly that my client has stopped working. That method works at OnStop event.

My actual problem is that what if All of the stuff above is not able to call OnDisconnected method on activity stop or the application closed.

Is there anything I am missing which is not letting it to happen.

UPDATE: I have tried changing the Transport level to WebSocket but it is not provided in Xamarin SDK for SignalR as mentioned in the intellisense . enter image description here

Community
  • 1
  • 1
Asim Khan
  • 572
  • 6
  • 34
  • Which transport do you use to connect to SignalR server? Try to use something like `hubConnection.Start(new WebSocketTransport())` Will it help? – Igor Lizunov Dec 09 '16 at 16:45
  • it uses .NetFramework 4.5 – Asim Khan Dec 09 '16 at 19:06
  • 1
    I don't mean platform. I mean transport which is specified at client-side. Please check [this link](https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#transport) as reference. The default transport is LongPollingTransport which works little bit unexpectedly, just as you described :-) The WebSocketTransport should solve you problem, but I know in there are significant issues with web sockets in Xamarin. So just try to check this. – Igor Lizunov Dec 10 '16 at 07:31
  • i have tried to add transport level but it seems websocket is not provided in Xamarin Android. Can I try ServerSent events to serve the purpose. I have updated the question ? – Asim Khan Dec 10 '16 at 12:06

1 Answers1

2

Since WebSocketTransport is not available at Xamarin, I advice to use "Ping" workaround.

  1. Implement Ping() method at server-side.
  2. Call this method from clients periodically (say 1/2 of the timeout interval).
  3. Within this method save ConnectionId : DateTime key/value pair to static ConcurrentDictionary at server-side.
  4. Run background Task at server-side and check DateTime for all dictionary keys.
  5. Remove old-ones and call appropriate code.
Igor Lizunov
  • 539
  • 5
  • 12
  • ping is very in-efficient way , especially while using some data-limited networks like some layers involved – Asim Khan Jan 12 '17 at 12:09