Jader Dias Jader Dias - 4 years ago 101
C# Question

How to detect a disconnection as soon as it happens on ASP.NET without polling?

I have a server-sent events handler in ASP.NET

Response.ContentType = "text/event-stream";
while (true)
{
if(thereIsAMessage)
{
Response.Write(message);
Response.Flush();
if (Response.IsClientConnected == false)
{
break;
}
}

System.Threading.Thread.Sleep(1000);
}


The problem is that I can only detect a client disconnection when I send something to the client. And I don't want to poll it, which defeats the whole purpose of using Server-sent events in the first place.

Answer Source

Try to look at SignalR - useful for long polling, server notifications won't be difficult to implement. Uses websockets when available. Yours scenario can be implemented very easily using hubs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download