当新记录自动插入表中时,不使用 jquery/ajax 来更新页面的另一种方法是什么
我使用 jquery、php 和 mysql 编写了一个小型聊天系统;但是,我正在寻找某种技术,只有在将新记录插入行中时才会更新 a 。我觉得每秒使用 jquery ajax 调用来检索新记录在我的服务器上确实是过度杀伤和费力的。
I've wrote a small chat system using jquery, php, and mysql; however, I'm looking for some kind of technology that will only update a if a new record is inserted into a row. I feel like using jquery ajax calls every second to retrieve new records is really overkill and strenuous on my server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在寻找 Comet 解决方案:http://en.wikipedia.org/wiki/Comet_ %28programming%29
正如 pdr 所指出的,这个想法是让 javascript 不断地向服务器打开一个异步请求。服务器将其保持打开状态,但在确定有内容要发送之前不会发送任何内容。 10-20 秒后,请求将在 javascript 端超时,之后应重新打开连接。
这使用基于“订阅者”的模型,通过该模型,服务器将一次性向所有订阅的客户端发送聊天消息或您拥有的内容。这可以节省许多数据库请求,因为请求请求的是服务器,而不是各个客户端。
You are looking for a Comet solution: http://en.wikipedia.org/wiki/Comet_%28programming%29
The idea, as pdr noted, is to the javascript continuously open an async request with the server. The server holds it open, but does not send anything until it determines there is something to send. The request will timeout on the javascript side after 10-20 seconds, after which it should re-open the connection.
This uses a 'subscriber' based model, by which the server will send out the chat message or what have you, to all clients which are subscribed, all at once. This saves you many database requests, as the server is the one asking for the requests, not the individual clients.
您想要的是长轮询。基本上,您创建一个 XHR,服务器和 PHP 会保持请求打开,直到新数据准备好发回为止。
您需要将 Apache 配置为在这种情况下不会超时,因此请进行一些实质性研究。基本上,PHP 看起来像这样...
然后,您为此页面创建一个 XHR,它将保持打开状态,直到新数据准备就绪。然后,在完成回调时,更新页面的状态并创建新的 XHR。
这比使用 XHR 持续轮询更新要高效得多。
确保你做了很多研究,因为我相信如果 PHP 脚本在 30 秒左右还没有停止,Apache 会认为出了问题。 :)
What you want is long polling. Basically, you make an XHR, and the server and PHP holdes the request open until new data is ready to be sent back.
You need to configure Apache not to timeout in this circumstances, so do some substantial research. Basically, the PHP looks like so...
Then, you make an XHR for this page, and it will stay open until new data is ready. Then, on the complete callback, update your page's state and make a new XHR.
This is a lot more efficient than polling for updates continually using XHR.
Make sure you do a lot of research because I believe Apache is going to think things are wrong if a PHP script hasn't stopped after 30 seconds or so. :)
我知道有几条路线可供您选择。
长轮询。浏览器在此处打开与服务器的连接,并且在服务器响应之前不执行任何操作。一旦服务器响应或超时(向浏览器发送空响应),就会发出新的长轮询请求。
采用此路线时,您应该使用不依赖于为每个请求使用新线程的服务器。
Web 套接字。同样,您需要一个能够处理请求而无需为每个请求生成新线程的服务器。使用 Web 套接字,客户端和服务器之间的连接保持打开状态,并且与长轮询不同,连接不会超时。但是,这还没有得到很好的支持。
我强烈建议您查看 http://socket.io/
There are a couple routes I know of that you can take.
Long polling. This is where the browser opens a connection to the server and does nothing until the server responds. Once the server responds or times-out (sends an empty response to the browser), a new long-polling request is made.
When going this route, you should use a server that does not rely on using a new thread for each request.
Web sockets. Again, you'll want a server that can handle requests without spawning a new thread every request. With web sockets, a connection is kept open between the client and servier, and unlike Long polling, doesn't time out. However, this isn't well-supported yet.
I highly recommend checking out http://socket.io/
Ajax 的要点在于它是异步的。您不能只是在服务器上等待,直到有值得发送的响应为止吗?
The point of Ajax is that it's asynchronous. Can you not just wait at the server until there's a worthwhile response to send?
对于标准 HTML/CSS/JS,这几乎是唯一的方法,因为浏览器可以向服务器发出请求,反之亦然。 AJAX 调用根本不必很大。根据定义,聊天系统需要频繁访问服务器。
With standard HTML/CSS/JS, that's pretty much the only way since the browser can make requests of the server, not vice versa. The AJAX call shouldn't have to be very big at all. A chat system, by definition, is going to require hitting the server a lot.