网络聊天客户端应该多久检查一次更新?

发布于 2024-08-12 23:02:10 字数 244 浏览 3 评论 0原文

我正在为一个应用程序设计一个基于网络的聊天功能,该功能的工作方式类似于 gmail 中的 facebook 聊天或 google talk。我想知道是否有人对应该多久检查一次更新有任何建议?每2秒一次?每5秒一次? 10秒?

另外,我应该在setTimeout的回调函数中调用setTimeout,还是在ajax请求更新的回调中调用setTimeout?基本上,调用之间应该有 n 秒,还是上一个请求完成和下一个请求开始之间有 n 秒?

谢谢

I am designing a web based chat feature for an application that will work sort of like facebook chat or google talk inside of gmail. I'm wondering if anyone has any advice on how often it should check for updates? Every 2 seconds? Every 5 seconds? 10 seconds?

Also, should I call setTimeout in the callback function of setTimeout, or in the callback of the ajax request for updates? Basically, should it be n seconds between calls, or n seconds between the completion of the last request and start of the next?

Thanks

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

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

发布评论

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

评论(4

你好,陌生人 2024-08-19 23:02:10

您可以使用 Comet (又名反向 AJAX)之类的东西来建立持久连接多次 ping 服务器。

否则,我认为 2-3 秒应该是一个不错的范围。任何较低的值都会给服务器带来过度的压力,而任何更长的值都会让应用程序感觉缓慢。

You could use something like Comet (AKA reverse AJAX) to have a persistent connection instead of pinging the server multiple times.

otherwise, I would say 2-3 seconds should be a good range. anything lower would be putting undue stress on the server, and anything longer would make the app feel sluggish.

缪败 2024-08-19 23:02:10

我会让它充满活力。确定一个您不想超出的范围,例如 2 到 10 秒,然后从某个中间点开始,例如 5 秒。如果您的民意调查总是返回新消息,请将等待时间缩短到最短时间。如果民意调查开始返回空值,请增加到最大值。这将为您在需要时的响应性和无任何情况时让服务器休息之间提供一个公平的平衡。

I would make it dynamic. Determine some range that you don't want to go out of, like 2 to 10 seconds, and start at some mid point, say 5 seconds. If your polls are always returning new messages, decrease the wait period toward your minimum. If polls start returning empty, increase toward your maximum. This will give you a fair balance between responsiveness when needed and giving your server a break when nothing much is going on.

回心转意 2024-08-19 23:02:10

您是否研究过基于事件的方法?这样,您将获得实时行为,节省资源,并且无需制定智能轮询间隔。

如果出于某种原因您需要使用轮询,我会选择较短的启动间隔,该间隔大约是您期望平均发生通知的时间长度的两倍,然后如果没有什么新的情况,则不时增加间隔。避免不必要地破坏您的服务器。但总的来说,一个好的答案实际上取决于您的系统的功能。

对于聊天来说,基于事件是理想的方式。

Have you looked into an event based approach? That way you will get real-time behavior, save resources and save you the need for coming up with a smart polling interval.

If for whatever reason you need to use polling I would go with a short start-interval which is about double the length of time that you expect a notification to occur on average and then increase the interval from time to time if there was nothing new to avoid hammering your server unnecessarily. Though in general a good answer really depends on what your system does.

For chat, event based is the ideal way to go imho.

窝囊感情。 2024-08-19 23:02:10

但它经常更新。

这意味着您可以分析自己的系统,并确定检查更新与负载的最佳时间。

However often it updates.

What this means is that you can profile your own system, and determine the optimal time to check for updates vs load.

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