刷新聊天框,当接收到新消息 [英] Refresh chat box when new msg received

查看:133
本文介绍了刷新聊天框,当接收到新消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PHP。我打算让使用PHP,一个聊天框一个简单的Web应用程序。

I'm new to PHP. I'm going to make a simple web application using PHP, a chat box.

我不知道一个更好的方式来尽快新消息收到刷新聊天窗口。即进入我的脑海里唯一的一点是令人耳目一新页频繁。我知道这是不是一个好主意。

I don't know a better way to refresh the chat window as soon as new msg received. The only thing that comes into my mind is refreshing page frequently. I know it's not a good idea.

我知道如何使用AJAX,这样我就可以只刷新聊天框(无需刷新整个页面)。但在AJAX也,客户端发送一个请求。

I know how to use AJAX, so I can refresh only the chat box (without refreshing whole page). But in AJAX also, client has to send a request.

有没有办法通过,当收到一个新的味精服务器刷新聊天框?

Is there a way to refresh the chat box by the server when a new msg received?

推荐答案

在Web应用程序的标准通信顺序是HTTP请求的Web客户端(浏览器)发起,然后由服务器响应。

The standard communication order in web applications is that HTTP requests are initiated by the web client (browser) and then responded by the server.

什么,你需要的是,角色互换和服务器计算机的请求的客户机接收消息。

What you would need is that the roles are reversed and the server machine requests the client machine to receive a message.

这就是所谓的服务器推送。链接的维基百科文章列出了许多解决方法。

This is called a server push. The linked Wikipedia article lists a lot of workarounds.

反复询问服务器是否有新的信息已经到达客户端是最简单的方法,它被称为轮询,但以高频率进行使应力在服务器上(乘以如果多个客户端执行此)中,用一个低频率是没有足够的反应对于许多使用案例。

The client repeatedly asking the server if a new message has arrived is the simplest method, it is called polling, but done with a high frequency puts stress on the server (multiplied if several clients do this) and with a low frequency is not responsive enough for many use cases.

尽管使用的老乡用户emsch大字体的WebSockets并不适合每一个人(还)是可行的,因为不是每个浏览器都支持它。 比较你的浏览器/操作系统矩阵如与浏览器的实现

Despite the big font used by fellow user emsch, WebSockets are not feasible for everyone (yet), as not every browser supports it. Compare your browser/os matrix e.g. with browser implementation.

我最喜欢的一对夫妇几年前是 BOSH ,我preferred其他如彗星方法:BOSH需要为服务器是服务器保持连接响应和对服务器的客户机是响应一个可能的第二连接。由于超时可能发生的,空的交流与一段时间后,低频表现。所以,如果没有邮件到达服务器或客户端,波什就像一个缓慢的查询。

My favourite a couple of years ago was BOSH, which I preferred to other methods like Comet: BOSH needs a connection held by the server for the server to be responsive and a potential second connection to the server for the client to be response. As timeouts can happen, empty exchanges are performed with a low frequency after some time. So if no messages arrive at the server or client, BOSH behaves like a slow polling.

如果你是新的网站和网络的发展,我建议找一个不错的消息库。

If you are new to web and network development I would suggest to look for a nice messaging library.

  • 如果您需要支持旧的浏览器,支持多种的提到的技术,并回落至最佳应用案例库
  • 如果你可以用现代的浏览器,去一些的WebSockets基础库。

这篇关于刷新聊天框,当接收到新消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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