简体   繁体   English

收到新的味精时刷新聊天框

[英]Refresh chat box when new msg received

I'm new to PHP. 我是PHP新手。 I'm going to make a simple web application using PHP, a chat box. 我将使用一个聊天框PHP创建一个简单的Web应用程序。

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. 我知道这不是一个好主意。

I know how to use AJAX, so I can refresh only the chat box (without refreshing whole page). 我知道如何使用AJAX,因此我只能刷新聊天框(而不刷新整个页面)。 But in AJAX also, client has to send a request. 但是在AJAX中,客户端也必须发送请求。

Is there a way to refresh the chat box by the server when a new msg received? 当收到新的味精时,服务器是否可以刷新聊天框?

Yes, there is a way to notify the client by the server. 是的,有一种方法可以通过服务器通知客户端。 It is called 叫做

WebSocket . WebSocket

With this technology you can send data from the server to the client and vice versa at any time. 使用此技术,您可以随时将数据从服务器发送到客户端,反之亦然。 It creates a TCP connection and keeps it open until you close it manually. 它会创建一个TCP连接并保持打开状态,直到您手动关闭它为止。

I have not used it in php before, but a quick google search gave me some results of libraries, so you should find a proper solution. 我以前没有在php中使用过它,但是谷歌快速搜索给了我一些库的结果,因此您应该找到一个合适的解决方案。 Though, I think a node.js server, on some other continuus running server, would fit better for this functionality. 但是,我认为在其他连续运行的服务器上,node.js服务器更适合此功能。

On client-side you can then communicate via the WebSocket in JavaScript. 然后,可以在客户端通过JavaScript中的WebSocket进行通信。
Open the connection first 首先打开连接

var webSocket = new WebSocket("ws://www.example.com/socketserver");

Then you can wait for incoming traffic and handle the data in a function 然后,您可以等待传入流量并处理函数中的数据

webSocket.onmessage = function (event) {
    console.log(event.data);
}

This is just a very short overview. 这只是一个简短的概述。 You will find a lot of information about this topic, for example in the Mozilla Developer Network 您将找到有关此主题的很多信息,例如在Mozilla开发人员网络中

The standard communication order in web applications is that HTTP requests are initiated by the web client (browser) and then responded by the server. Web应用程序中的标准通信顺序是HTTP请求由Web客户端(浏览器)发起,然后由服务器响应。

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. 链接的Wikipedia文章列出了许多解决方法。

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. 客户端反复询问服务器是否有新消息是最简单的方法,称为轮询,但是如果操作频率较高,则会给服务器带来压力(如果有多个客户端这样做,则会成倍增加),而响应频率不够低对于许多用例。

Despite the big font used by fellow user emsch, WebSockets are not feasible for everyone (yet), as not every browser supports it. 尽管其他用户emsch使用了大字体,但WebSockets尚不适用于所有人(因为),因为并非每个浏览器都支持它。 Compare your browser/os matrix eg with browser implementation . 将您的浏览器/操作系统矩阵与浏览器实现进行比较

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. 几年前我最喜欢的是BOSH ,我更喜欢Comet之类的其他方法:BOSH需要服务器拥有的连接才能使服务器响应,而可能需要与服务器建立第二个连接才能使客户端响应。 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. 因此,如果没有消息到达服务器或客户端,则BOSH的行为类似于慢速轮询。

If you are new to web and network development I would suggest to look for a nice messaging library. 如果您不熟悉Web和网络开发,建议您寻找一个不错的消息库。

  • If you need to support older browsers a library which supports several of the mentioned techniques and falls back to the best applicable case 如果您需要支持较旧的浏览器,则该库支持上述几种技术,并且会退回到最适用的情况
  • If you can use modern browsers, go for some WebSockets based lib. 如果可以使用现代浏览器,请使用一些基于WebSocket的库。

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

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