title: How to detect a disconnection when using Server-sent events on ASP.NET?
body: <p>I have the following SSE handler in ASP.NET</p>
  
  <pre><code>Response.ContentType = "text/event-stream";
  while (true)
  {
      Response.Write(string.Format("data: {0}\n\n", DateTime.Now.ToString()));
      Response.Flush();
      System.Threading.Thread.Sleep(1000);
  }
  </code></pre>
  
  <p>It runs indefinitely, even after I closed the client application. How to notify the server to stop the handler?</p>
  
  <p>I tried on the client:</p>
  
  <pre><code>var source = new EventSource('Handler.aspx');
  window.onunload = function() {
       source.close();
  }
  </code></pre>
  
  <p>But I didn't succeed.</p>