title: SignalR - Server side method to detect if a client disconnects from a hub?
body: <p>I'm wanting to stop a <code>System.Timers.Timer</code> that is running in a SignalR hub after a client closes a window/tab containing the active connection. </p>
  
  <p>I have tried sending a bool value to the server by calling server code to notify the server the client is still connected or not, but it's not currently working. </p>
  
  <pre><code>window.onbeforeunload = function () {
      profile.server.setIsConnected(false);
  };
  </code></pre>
  
  <p>Server Side:</p>
  
  <pre><code>public ProfileHub()
  {  
      timer = new Timer(15000);
      timer.Elapsed += (sender, e) =&gt; { timer_Elapsed(sender, e, _isActive); };
      timer.AutoReset = false;
  }
  
  [Authorize]
  private void timer_Elapsed(object sender, ElapsedEventArgs e, bool active)
  {            
      timer.Stop();
  
      if (active)
      {
          System.Diagnostics.Debug.WriteLine("Timer Started");
          timer.Start();
      }
      else
      {
          System.Diagnostics.Debug.WriteLine("Timer Stopped");
          return;
      }
      // process code
  }
  
  [Authorize]
  public void SetIsActive(bool isActive)
  {
      _isActive = isActive;
  }
  </code></pre>
  
  <p>Is this possible and am I on the right track? I suspect it has something to do with the anonymous delegate for <code>timer.Elapsed</code>, but I'm not entirely sure.</p>