服务器轮询间隔为一个javascript聊天客户端 [英] Server polling intervals for a javascript chat client

查看:111
本文介绍了服务器轮询间隔为一个javascript聊天客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个基本很少的AJAX在线留言板/聊了我的网站,但我不知道究竟是如何实现服务器轮询。

I'm building a basic little AJAX shoutbox/chat for my website, but I'm not sure exactly how to implement the server polling.

下面是基本的程序流程我在想:

Here's the basic program flow I'm thinking of:

  1. 用户涉及到网页,并显示最近10消息
  2. 要获得别人发送的消息,JavaScript的将请求的URL与时间戳参数的客户端(设置为客户端接收到的最后一条消息的值)
  3. 在服务器返回的所有消息(高达10 A最大),因为该时间戳。

唯一的问题是如何经常​​轮询服务器。显然,这应查询新的消息添加每一次,但是当你只是阅读别人的信息,它需要自动更新。

The only issue is how often to poll the server. Obviously it should poll each time a new message is added, but when you're just reading others' messages it needs to automatically update.

这应该是一个设定的时间限制?如:每10秒。或者说,它应该根据使用有所不同?例如:5秒后检查。如果没有消息,不检查另外10秒。如果还是有没有新的消息,请在15秒,然后20,最多可能每隔30秒以内。检测到的每一次有新的消息,重置计时器回落至5秒,再次启动。

Should it be a set time limit? eg: every 10 seconds. Or, should it vary depending on usage? eg: Check after 5 seconds. If there's no messages, don't check for another 10 seconds. If there's still no new messages, check in 15 seconds, then 20, up to maybe once every 30 seconds max. Each time there's a new message detected reset your timer back down to 5 seconds and start again.

我只是担心把服务器上的不必要的压力,考虑到我们可能有数百个用户同时在线。

I'm just concerned about putting unnecessary stress on the server, considering that we could have hundreds of users concurrently online.

...还是我得到了整个事情错了吗?有没有更好的方式来实现一个基本的JavaScript聊天?

...or have I got the whole thing wrong? Is there a better way to implement a basic javascript chat?

推荐答案

彗星,由Brian描述是一个很好的技术,但需要在服务器,它可能比你愿意实现更高级的一个简单的聊天框上的会话支持

Comet, described by Brian is a nice technique, but requires session support on the server, which is probably more advanced than you care to implement for a simple chat box.

要实现轮询间隔最好的方法是想象你有一个聊天窗口,你可以尽量减少做其他的东西,或打开看,如果你有新邮件。当你在谈话中,你会切换到它(调查)频繁。如果你没有得到任何消息了一段时间,你就会开始寻找罕见,罕见的,直到你只检查它偶尔为之。

The best way to implement polling intervals is to imagine you having a chat window which you can minimize to do other stuff, or open to see if you have new messages. When you are in the middle of a conversation, you'll switch to it (poll) frequently. If you don't get any messages for a while, you will start looking rarer and rarer until you only check it occasionally.

假设你不需要做实时打字,你也许可以当在活动高峰轮询每隔3秒左右,如果没有显示5-10投票,开始杀青的时间间隔(可能是加倍它的每一次),直到它击中30-60秒。收到一条消息回应该重新设置轮询间隔回了几秒钟,将消息发送应立即查询,但可能并不需要实现轮询频率,否则。

Assuming you don't need to do real-time typing, you can probably poll every 3 seconds or so when at peak activity, and if nothing shows up for 5-10 polls, start to crank the interval up (perhaps doubling it every time) until it hits 30-60 seconds. Getting a message back should reset the poll interval back to a few seconds, while sending a message should poll instantly, but probably doesn't need to effect the frequency of polling otherwise.

这篇关于服务器轮询间隔为一个javascript聊天客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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