如何在不轮询的情况下在 ASP.NET 上立即检测到断开连接? [英] How to detect a disconnection as soon as it happens on ASP.NET without polling?

查看:60
本文介绍了如何在不轮询的情况下在 ASP.NET 上立即检测到断开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.NET 中有一个 服务器发送事件 处理程序

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.

推荐答案

试试看 SignalR - 长期有用轮询,服务器通知不会很难实现.在可用时使用 websockets.您可以使用集线器轻松实现您的场景.

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.

这篇关于如何在不轮询的情况下在 ASP.NET 上立即检测到断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆