title: How to use SignalR events to keep connection alive in the right way?
body: <p>I am developing a real-time client-server application using SignalR, ASP.NET and C#. I am using localhost as host and VS2013.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Why if I <b>close</b> server, on web-client the "<b>Reconnect</b>" event occurs?</p></li>
  <li><p>The "Disconnect" event occurs after <b>40+ seconds</b> only. How to reduce this time?</p></li>
  <li><p>I need the client to connect to server on start. The "Reconnect" event should occurs within <b>fixed interval</b> only. If "Reconnect" interval time is over the client should connect as a new client. How to archive this goal?</p></li>
  </ol>
  
  <p>Finally, I would like to ask - how to <b>keep alive</b> connection using <b>SignalR</b> in the <strong>right way</strong>? </p>
  
  <p>I am using this code:</p>
  
  <p>C#</p>
  
  <pre><code>public override Task OnDisconnected()
  {
  clientList.RemoveAt(nIndex);
  Console.WriteLine("Disconnected {0}\n", Context.ConnectionId);
  return (base.OnDisconnected());
  }
  
  public override Task OnReconnected()
  {
  Console.WriteLine("Reconnected {0}\n", Context.ConnectionId);
  return (base.OnReconnected());
  }
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>$.connection.hub.reconnected(function () {
  // Html encode display name and message.
  var encodedName = $('&lt;div /&gt;').text("heartbeat").html();
  var now = new Date();
  
  // Add the message to the page.
  $('#discussion').append('Reconnected to server: ' + now + '&lt;/br&gt;');
  });
  
  $.connection.hub.disconnected(function () {
  // Html encode display name and message.
  var encodedName = $('&lt;div /&gt;').text("heartbeat").html();
  var now = new Date();
  // Add the message to the page.
  $('#discussion').append('Disconnected from server: ' + now + '&lt;/br&gt;');
  });
  </code></pre>
  
  <p>After Connect output:</p>
  
  <pre><code>message received from server : Fri Feb 21 2014 10:53:02
  </code></pre>
  
  <p>After Close Server output:</p>
  
  <pre><code>Reconnected to server: Fri Feb 21 2014 10:53:22 &lt;-- Why, if i close server ???
  Disconnected from server: Fri Feb 21 2014 10:53:53 &lt;-- Why 40+ seconds after the server is closed ?
  </code></pre>