gpt4 book ai didi

javascript - 使用 JavaScript 进行服务器轮询

转载 作者:行者123 更新时间:2023-12-03 21:37:08 25 4
gpt4 key购买 nike

使用 JavaScript 轮询服务器有哪些好的做法需要非常快速地刷新数据的应用程序?我使用 jQuery 作为前端,使用 Java Spring Framework 作为后端。

刷新数据的示例可以是快速更新的项目列表(每 1 秒)。

最佳答案

您可能想使用jQuery's Ajax functions每隔一秒左右轮询一次服务器。然后服务器可以近乎实时地向浏览器响应指令。

您也可以考虑long polling而不是上述,在不增加轮询频率的情况下减少延迟。

引用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 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.

除了上述内容之外,我还建议您查看以下 Stack Overflow 帖子的已接受答案,以获取长轮询技术的详细说明:

关于javascript - 使用 JavaScript 进行服务器轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3583203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com