http 服务器可以检测到客户端已取消其请求吗?

发布于 2024-08-29 07:06:11 字数 669 浏览 3 评论 0原文

我的网络应用程序必须处理和提供大量数据才能显示某些页面。有时,用户关闭或刷新页面,而服务器仍在忙于处理该页面。这意味着服务器将继续处理数据几分钟,然后将其发送给不再侦听的客户端。

是否可以检测到连接已断开并做出反应?

在这个特定的项目中,我们使用 Django 和 NginX,或者 Apache。我认为这是可能的,因为 Django 开发服务器似乎通过打印 Broken Pipe 异常来对取消的请求做出反应。我希望它引发一个我的应用程序代码可以捕获的异常。 看来 JSP 可以做到这一点node.js 也可以 此处

或者,我可以在相关页面上注册一个卸载事件处理程序,让它执行同步 XHR 请求取消来自该用户的先前请求,并执行某种进程间通信来实现此目的。也许如果将较慢的数据处理交给另一个进程,我可以更轻松地识别和杀死它,而无需杀死响应的进程......

My web app must process and serve a lot of data to display certain pages. Sometimes, the user closes or refreshes a page while the server is still busy processing it. This means the server will continue to process data for several minutes only to send it to a client who is no longer listening.

Is it possible to detect that the connection has been broken, and react to it?

In this particular project, we're using Django and NginX, or Apache. I assumed this is possible because the Django development server appears to react to cancelled requests by printing Broken Pipe exceptions. I'd love to have it raise an exception that my application code could catch. It appears JSP can do this. So can node.js here.

Alternatively, I could register an unload event handler on the page in question, have it do a synchronous XHR requesting that the previous request from this user be cancelled, and do some kind of inter-process communication to make it so. Perhaps if the slower data processing were handed to another process that I could more easily identify and kill, without killing the responding process...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-09-05 07:06:11

虽然 @Oded 认为 HTTP 在请求之间是无状态的,但应用服务器确实可以检测到正在处理的请求的底层 TCP/IP 连接何时中断。这是为什么呢?因为 TCP 是一种用于可靠连接的有状态协议。

.Net Web 应用程序处理资源密集型请求的常用技术是检查 Response.IsClientConnected (docs) 在开始资源密集型工作之前。浪费 CPU 周期向不再存在的客户端发送昂贵的响应是没有意义的。

private void Page_Load(object sender, EventArgs e)
{
    // Check whether the browser remains
    // connected to the server.
    if (Response.IsClientConnected)
    {
        // If still connected, do work
        DoWork();
    }
    else
    {
        // If the browser is not connected
        // stop all response processing.
        Response.End();
    }
}

请回复您的目标应用程序服务器堆栈,以便我可以提供更相关的示例。

关于使用 XHR 将客户端页面卸载事件发布到服务器的第二种选择,@Oded 关于 HTTP 在请求之间是无状态的评论是正确的。这不太可能起作用,尤其是在具有多个服务器的场中。

While @Oded is correct that HTTP is stateless between requests, app servers can indeed detect when the underlying TCP/IP connection has broken for the request being processed. Why is this? Because TCP is a stateful protocol for reliable connections.

A common technique for .Net web apps processing a resource intensive request is to check Response.IsClientConnected (docs) before starting the resource intensive work. There is no point in wasting CPU cycles to send an expensive response to a client that isn't there anymore.

private void Page_Load(object sender, EventArgs e)
{
    // Check whether the browser remains
    // connected to the server.
    if (Response.IsClientConnected)
    {
        // If still connected, do work
        DoWork();
    }
    else
    {
        // If the browser is not connected
        // stop all response processing.
        Response.End();
    }
}

Please reply with your target app server stack so I can provide a more relevant example.

Regarding your 2nd alternative to use XHR to post client page unload events to the server, @Oded's comment about HTTP being stateless between requests is spot on. This is unlikely to work, especially in a farm with multiple servers.

偏闹i 2024-09-05 07:06:11

HTTP 是无状态的,因此检测断开连接的客户端的唯一方法是通过超时。

请参阅这个 SO问题的答案(Java Servlet:如何检测浏览器关闭?)。

HTTP is stateless, hence the only way to detect a disconnected client is via timeouts.

See the answers to this SO question (Java Servlet : How to detect browser closing ?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文