使用 JavaScript 进行服务器轮询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server polling with JavaScript
提问by newbie
What is best practise for polling server with JavaScript for application that needs to refresh data very rapidly? I'm using jQuery for front-end and Java Spring Framework for backend.
对于需要非常快速刷新数据的应用程序,使用 JavaScript 轮询服务器的最佳做法是什么?我在前端使用 jQuery,后端使用 Java Spring Framework。
Example of refreshed data could be list of items that are getting updated very rapidly (every 1 second).
刷新数据的示例可以是快速更新的项目列表(每 1 秒)。
回答by Daniel Vassallo
You may want to use jQuery's Ajax functionsto poll the server every second or so. Then the server can respond with instructions to the browser in near real-time.
您可能希望使用jQuery 的 Ajax 函数每隔一秒左右轮询一次服务器。然后服务器可以近乎实时地向浏览器响应指令。
You can also consider long pollinginstead of the above, to reduce the latency without increasing the frequency of the polls.
您还可以考虑长轮询而不是上述方式,以在不增加轮询频率的情况下减少延迟。
Quoting Comet Daily: The Long-Polling Technique:
The long-polling Comet technique is a technique that optimizes traditional polling to reduce latency.
Traditional polling sends an
XMLHttpRequestto the server in fixed intervals. For example, open a newXMLHttpRequestevery 15 seconds, receive an immediate response, and close the connection.Long-polling sends a request to the server, but a response is not returned to the client until one is available. As soon as the connection is closed, either due to a response being received by the client or if a request times out, a new connection is initiated. The result is a significant reduction in latency because the server usually has a connection established when it is ready to return information to return to the client.
长轮询 Comet 技术是一种优化传统轮询以减少延迟的技术。
传统轮询
XMLHttpRequest以固定时间间隔向服务器发送一个。例如,XMLHttpRequest每 15 秒打开一个新的,收到立即响应,然后关闭连接。长轮询向服务器发送请求,但在响应可用之前不会向客户端返回响应。一旦连接关闭,无论是由于客户端收到响应还是请求超时,都会启动新连接。结果是延迟显着减少,因为服务器通常在准备好返回信息以返回给客户端时建立了连接。
In addition to the above, I also suggest that you check out the accepted answer to the following Stack Overflow post for a detailed description of the long polling technique:
除了上述内容,我还建议您查看以下 Stack Overflow 帖子的已接受答案,以获取有关长轮询技术的详细说明:
回答by Gordon
I second Daniel's suggestionto use long-poll or push. Check out
CometD is a scalable HTTP-based event routing bus that uses a Ajax Push technology pattern known as Comet. The term 'Comet' was coined by Alex Russell in his post Comet: Low Latency Data for the Browser.
CometD 是一种可扩展的基于 HTTP 的事件路由总线,它使用称为 Comet 的 Ajax Push 技术模式。“彗星”一词是亚历克斯·拉塞尔在他的帖子彗星:浏览器的低延迟数据中创造的。
They have a page explaining how to get that work with Spring:
他们有一个页面解释了如何使用 Spring 来完成这项工作:
回答by Simon Rigét
As of 2018 you should use the fetch function with promise syntax:
从 2018 年开始,您应该使用具有 promise 语法的 fetch 函数:
<script type="text/javascript">
setInterval(function(){
fetch("your_serverside_script.php") // Any output from the script will go to the "result" div
.then(response => response.text())
.catch(error => document.getElementById("result").innerHTML = error)
.then(response => document.getElementById("result").innerHTML = response)
}, 1000); // Poll every 1000ms
</script>
<div id="result">result will appear here</div>

