I finally figured it out. Here is what I learned from the beginning of this question:
Background:. We are creating an iOS application using Xamarin / Monotouch and .NET SignalR 2.0.3. We use SignalR protocols by default - and it looks like it uses SSE instead of web sockets. I'm not sure if you can use web sockets with Xamarin / Monotouch. Everything is hosted on Azure sites.
We needed an application to quickly connect to our SignalR server, but we still had problems when the connection did not reconnect on our own - or reconnecting took exactly 30 seconds (due to the main protocol timeout).
There were three scenarios in which we ended up testing:
Scenario A - Connection at the first boot of the application. It worked flawlessly from day one. The connection is completed in less than .25 seconds, even via 3G mobile connections. (if the radio is already on)
Scenario B - reconnecting to the SignalR server after the application has been idle / closed for 30 seconds. In this case, the SignalR client will eventually reconnect to the server without any special work - but it seems to wait exactly 30 seconds before trying to reconnect. (too slow for our application)
During this 30 second waiting period, we tried to call HubConnection.Start (), which did not affect. And calling HubConnection.Stop () also takes 30 seconds. I found a related error on the SignalR site that seems to be fixed , but we still have the same problem in version 2.0.3.
Scenario C - reconnecting to the SignalR server after the application has been idle / closed for 120 seconds or longer. In this case, the SignalR transport protocol is already disabled, so the client will never automatically reconnect. This explains why the client is sometimes, but not always, rebuilt on its own. The good news is that calling HubConnection.Start () works almost instantly, like script A.
Therefore, it took me a while to understand that the conditions for reconnecting differ depending on whether the application was closed for 30 seconds versus 120 + seconds. Although the SignalR trace logs highlight what is happening with the underlying protocol, I don’t believe that there is a way to handle transport layer events in the code. (The Closed () event fires after 30 seconds in script B, immediately in script C, the State property says “Connected” during these periods of waiting for reconnection, no other relevant events or methods)
Solution: The solution is obvious. We are not waiting for SignalR to do the reconnection magic. Instead, when the application is activated or when the telephone network recovery is restored, we simply clear the events and delete the links to the HubConnection (we cannot dispose of it because it takes 30 seconds, I hope the garbage collection takes care of this) and creates a new instance. Now everything works fine. For some reason, I thought we should reuse a persistent connection and reconnect, not just create a new instance.