简体   繁体   English

如何加快jquery / php / ajax聊天室的速度?

[英]How to speed up a jquery/php/ajax chatroom?

I've created a small jquery and php chatroom with some .get and .post functions and php docs that read and write data to a sql server. 我已经创建了一个小的jquery和php聊天室,其中包含一些.get和.post函数以及用于读取和写入数据到sql server的php文档。 It works fine, but the small problem is when someone posts something, it takes about half a second for it to appear (because of the lag). 它可以正常工作,但是一个小问题是,当某人发布某些内容时,它大约需要半秒钟才能显示出来(由于滞后)。 I fear there's something wrong with my coding. 我担心我的编码有问题。 im using 我正在使用

setinterval (listen, 300)

as my continuous jquery function for reading new db entries, listen is a function with a .get inside. 作为用于读取新数据库条目的连续jquery函数,listen是一个内置.get的函数。 How does stackoverflow or facebook do it so that the user types something in and immediately it pops out? stackoverflow或facebook如何做到这一点,以便用户输入内容并立即弹出?

Maybe try displaying the inputted chat message immediately to the user who posted it, prior to posting it to the database. 也许尝试将输入的聊天消息立即显示给发布它的用户,然后再将其发布到数据库中。

Like this: 像这样:

  • User enters message, submits 用户输入消息,提交
  • Update users chat window so they see it immediately 更新用户聊天窗口,以便他们立即看到它
  • POST message to database POST消息到数据库
  • GET from db and update all chat windows 从数据库获取并更新所有聊天窗口

This can be implemented using various techniques, which have many names: Long-polling, Server Sent Events, Comet, WebSockets, and others. 这可以使用各种技术来实现,这些技术有许多名称:长轮询,服务器发送事件,Comet,WebSockets等。

Basic idea is this: 基本的想法是这样的:

Alice opens facebook. 爱丽丝打开Facebook。 Her browser makes a request for updates ($.get, for example), but the server does not respond if there are no new updates and the request remains in 'waiting' state. 她的浏览器发出更新请求(例如$ .get),但是如果没有新的更新并且请求仍处于“等待”状态,则服务器不响应。

Bob opens facebook. 鲍勃打开Facebook。 He decides to comment on Alice's wall. 他决定评论爱丽丝的墙。 His browser posts his comment to the server ($.post). 他的浏览器将他的评论发布到服务器($ .post)。

The server accepts this post, handles it properly (saves onto Alice's wall, etc) But ALSO server checks if there is a waiting update request from Alice. 服务器接受此帖子,对其进行正确处理(保存在Alice的墙上等),但是ALSO服务器检查是否有来自Alice的等待更新请求。 If there is, server renders info about this update into response stream and closes the connection. 如果有,服务器将有关此更新的信息呈现到响应流中并关闭连接。 Alice's browser finally gets a response to this long hanging request and happily draws a red "1" in the notification area. Alice的浏览器终于​​得到了对这个长时间挂起请求的响应,并在通知区域中愉快地绘制了一个红色的“1”。 It also immediately opens another update request (to not miss any). 它还会立即打开另一个更新请求(不要错过任何更新请求)。

Alice sees comment from Bob, which was delivered instantly. 爱丽丝看到鲍勃的评论,立即发布。

The technique described is called "long polling" and it was first introduced by Google in Gmail. 所描述的技术称为“长轮询”,它最初由谷歌在Gmail中引入。

You can use HTML5 sockets, however these are very much in their infancy and not widely supported (ie by IE). 您可以使用HTML5套接字,但是这些套接字尚处于起步阶段,并未得到广泛支持(即IE)。

Lots of systems use Flash as a middle-man as that can hold a connection open. 许多系统将Flash用作中间人,因为它可以保持连接打开。

With either of these you can use your PHP code that stores the comment to the database to also push this out to every "listener". 使用其中任何一个,您都可以使用将代码存储到数据库的PHP代码,并将其推送到每个“侦听器”。 This will be the quickest way possible. 这将是最快捷的方式。 If your system is super clever it'll incorporate all three - HTML5 sockets where it can, Flash where it can't, and your regular polling were there's no Flash either. 如果你的系统非常聪明,那么它将包含所有三个 - HTML5套接字,闪存不可能,而你的常规轮询也没有Flash。

http://pusher.com/ might be a good starting point for further learning. http://pusher.com/可能是进一步学习的一个很好的起点。

Most sites that have fast chat use a technique called comet. 大多数具有快速聊天功能的站点都使用一种称为彗星的技术。 You can read more about it here: http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications 你可以在这里阅读更多相关信息: http//ajaxian.com/archives/comet-a-new-approach-to-ajax-applications

It essentially is a piece of modified server software that waits to return a response to the user until either a message is sent or it is about to timeout. 它本质上是一块经过修改的服务器软件,它等待向用户返回响应,直到发送消息或即将超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM