问题描述
什么是轮询服务器的JavaScript应用程序需要更新数据非常迅速最佳做法?我使用jQuery的前端和Java Spring框架的后端。
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.
刷新数据的例子可能是正在得到更新非常迅速的项目(每隔1秒)的清单。
Example of refreshed data could be list of items that are getting updated very rapidly (every 1 second).
推荐答案
您可能需要使用 jQuery的Ajax函数轮询服务器每秒左右。然后,服务器可以与指令以在近实时浏览器响应
You may want to use jQuery's Ajax functions to poll the server every second or so. Then the server can respond with instructions to the browser in near real-time.
您也可以考虑长轮询的代替上面,以减少等待时间而不增加民意调查的频率。
You can also consider long polling instead of the above, to reduce the latency without increasing the frequency of the polls.
引述彗星日报:长轮询技术:
在长轮询的Comet技术是优化传统轮询来降低延迟的技术。
传统的轮询发送 XMLHtt prequest
来在固定的时间间隔的服务器。例如,打开一个新的 XMLHtt prequest
每15秒,立即得到答复,并关闭连接。
Traditional polling sends an XMLHttpRequest
to the server in fixed intervals. For example, open a new XMLHttpRequest
every 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.
在除了上述,我也建议你看看接受的答案下面的堆栈溢出后为长轮询技术的详细描述:
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:
- How确实脸谱,Gmail中发送实时通知?
这篇关于使用JavaScript服务器轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!