自动重新建立长轮询请求的最佳方法是什么? [英] What is the best way to automatically reestablish long polling request?

查看:411
本文介绍了自动重新建立长轮询请求的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用长轮询进行项目,因为nginx服务器将在建立连接1分钟后响应504网关超时,我在$ .ajax中编写了错误处理函数当它收到504错误时它可以自动重新连接到服务器。但是虽然我已经这样做了,但我不知道这是否是长轮询机制的最佳实践,这将在日志中产生大量的504。这似乎不够好,所以我应该怎么做?

I am working on a project using long polling, as the nginx server will response 504 Gateway Timeout after the connection established 1 minute or so, I write the error handling function in the $.ajax so that it can automatically reconnect to the server when it receives 504 error. But although I have done this, I don't know whether this is the best practice of long polling mechanism, and this will produce a lot of 504 in log. It seems not good enough, so how should I do it?

推荐答案

你应该在漫长的民意调查中缩短时间响应。通常30秒或更短时间就可以了。如果您的服务器在30秒内没有要发送的数据,则必须发送空响应。然后,客户端将发出新请求(立即或暂停后,取决于您希望减少延迟的数量;例如,在Lightstreamer中,默认情况下我们不会在长轮询周期之间使用任何暂停)。

You should put a shorter time-out in the long polling response. Usually 30 seconds or less makes the trick. If within 30 seconds your server does not have data to send, it must send an empty response. The client will then issue a new request (immediately, or after a pause, depending on how much you want to reduce latency; for example, in Lightstreamer by default we don't use any pause between long-polling cycles).

这篇关于自动重新建立长轮询请求的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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