我知道,Response.IsClientConnected
但在我的情况下,它有很大的滞后性。代码:
// sample code for sending a dynamic file in chuncks
long bytesSent = 0;
while (continueSending)
{
if (!AskReceiverToContinue())
break;
if (!Response.IsClientConnected)
break;
// Your suggestion goes here... :)
try
{
Response.OutputStream.Write(buffer, 0, buffer.Length);
bytesSent += buffer.Length;
}
Catch
{ // To my experience, this is more reliable than Response.IsClientConnected
continueSending = false;
}
}
问题是客户端实际接收到的字节数比我的bytesSent
. 似乎当客户端断开连接时,我的程序发现情况有很大的滞后(并继续增加bytesSent
),这是因为 ASP.NET 告诉我情况(客户端断开连接)较晚。
是否有任何可靠的方法可以找出客户端何时断开(实时)?