AJAX Shoutbox 不使用间隔,可以做到吗?
好吧,我正在寻找一种方法,让喊话箱仅在任何人输入消息时才触发。
基本上,我们可以控制当有人提交喊叫时会发生什么,但我不想使用 Javascript 间隔(setTimeout()
或 setInterval()
)函数来每 x 秒调用一次。因为每隔 x 秒,它就会访问数据库,尝试确定是否发出了喊叫声。
必须有一个更好的方法来做到这一点,不会每隔 x 秒访问数据库一次,以便更新 Shoutbox 让每个人都能看到喊叫声。
这就是我需要帮助的地方,因为我们的shoutbox在服务器上使用了太多资源,导致服务器有时过载。
我考虑过使用一个文件来代替...例如,当喊叫被发布到喊叫箱时,服务器上的文件名会更改为某些内容,该文件名将被检查(而不是数据库),并且如果文件名不同,而不是应该增加喊叫声。也许,它甚至可以将喊叫放在文件中,而不是数据库中,并且仅在将另一个喊叫添加到喊叫箱后才将其粘贴到数据库中。因此,最后的呼喊将始终以文本格式保留在文件中。
这是处理服务器过载的更好方法吗?但即使如此,我仍然需要调用一个时间间隔来确定文件名是否更改。所以这仍然是调用一个间隔,但是你认为这样在服务器上会更好吗?如果是这样,我应该使用什么名称?我应该使用 php time()
函数来命名文件吗?
有人可以推荐其他任何想法来处理这个问题吗?最好是一种不使用任何间隔来更新shoutbox的方法?
请帮忙,谢谢大家:)
Ok, I am seeking a way for a shoutbox to trigger ONLY when a message is inputted into it by anyone.
Basically, we have control over what happens when someone submits a shout, but I don't want to use a Javascript interval (setTimeout()
or setInterval()
) functions to call this every x amount of seconds. Because every x amount of seconds it is hitting the database trying to determine if a shout has been made or not.
There MUST be a better method to do this that doesn't hit the database every x seconds in order to update the Shoutbox for everyone to see the shout.
This is where I need help, because our shoutbox is using too many resources on the server and causing the server to overload at times.
I have thought about a file instead... For example, when a shout has been posted to the shoutbox, a filename on the server changes to something, this filename than gets checked (instead of the database), and if the filename is different, than it should load up the shout. Perhaps, it can even place the shout within the file, instead of the database, and ONLY stick it into the database after another shout has been added to the shoutbox. Therefore, the last shout will always remain in the file, in text format.
Would this be a better approach for handling server overloads? But even still I would need to call an interval to determine if the file name changed. So this is still calling an interval, but do you think it would be better on the server this way? If so, what name should I use? Should I use the php time()
function for this to name the file?
Are there any other ideas that someone could recommend to handle this. Preferably a way without using any intervals to update the shoutbox?
Please help, thanks guys :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想研究 HTML5 的 WebSockets,但对于不兼容的浏览器,我认为您必须不断轮询服务器在一个间隔上。
You might want to look into HTML5's WebSockets, but for non-compatible browsers I think you're stuck with continuously polling the server on an interval.
您对可能的解决方案的描述所接近的更像是服务器推送而不是页面获取。
我知道您可以使用带有
Transfer-encoding: chunked
的 node.js 来实现这种行为。查看 http://www.nodejs.org/ 上的视频以获得更好的示例What you are getting close to with your descriptions of a possible solution is more like a server push than a page get.
I know you can achieve this sort of behavior using node.js with a
Transfer-encoding: chunked
. Check out the video at http://www.nodejs.org/ for a better example