ratchet教程
In this tutorial, we’ll be taking a look at Ratchet, a PHP library for working with WebSockets. Let’s start by defining what WebSockets are. MDN says:
在本教程中,我们将看一下Ratchet (一个用于处理WebSocketsPHP库)。 让我们开始定义什么是WebSockets。 MDN说:
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
WebSockets是一项先进的技术,可以打开用户浏览器和服务器之间的交互式通信会话。 使用此API,您可以将消息发送到服务器并接收事件驱动的响应,而不必轮询服务器以获取答复。
![title Connected computers image](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/09/1442180774Fotolia_90420718_Subscription_Monthly_M-1024x682.jpg)
WebSockets allow us to write applications that can pass data from the browser to the server and vice-versa in real-time.
WebSockets允许我们编写应用程序,这些应用程序可以实时将数据从浏览器传递到服务器,反之亦然。
建立 (Setup)
First, let’s install Ratchet using Composer:
首先,让我们使用Composer安装Ratchet:
composer require cboden/ratchet
构建应用 (Building the App)
Now we’re ready to build the app. Create a Chat.php
file under the class/ChatApp
directory. This would be a class under the ChatApp
namespace, and it would use use Ratchet’s MessageComponentInterface
and ConnectionInterface
. The MessageComponentInterface
is used as the basic building block for Ratchet applications, while the ConnectionInterface
represents the connection to the application.
现在我们准备构建该应用程序。 在class/ChatApp
目录下创建一个Chat.php
文件。 这将是ChatApp
命名空间下的类,并且将使用Ratchet的MessageComponentInterface
和ConnectionInterface
。 MessageComponentInterface
用作Ratchet应用程序的基本构建块,而ConnectionInterface
表示与应用程序的连接。
<?php
namespace ChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
Have the class implement the MessageComponentInterface
. This contains the blueprint for the methods that we need to implement such as onOpen
, onClose
and onMessage
.
让类实现MessageComponentInterface
。 这包含我们需要实现的方法的蓝图,例如onOpen
, onClose
和onMessage
。
class Chat implements MessageComponentInterface {
}
Inside the class, we declare a variable called $clients
. This is where we will store a list of currently connected clients in our chat app later on. From the constructor, you will see that we’re using SplObjectStorage
. This provides a way for us to store objects. In this case, the object that we need to store is the connection object for each client.
在类内部,我们声明一个名为$clients
的变量。 稍后,我们将在这里将当前连接的客户端列表存储在聊天应用程序中。 从构造函数中,您将看到我们正在使用SplObjectStorage
。 这为我们提供了一种存储对象的方法。 在这种情况下,我们需要存储的对象是每个客户端的连接对象。
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
Next, we implement the onOpen
method. This method is called every time a new connection is opened in the browser. What this does is store the new connection object using the attach
method. We also output that someone has connected as a means of testing if the onOpen
method is working correctly.
接下来,我们实现onOpen
方法。 每次在浏览器中打开新连接时,都会调用此方法。 这样做是使用attach
方法存储新的连接对象。 我们还输出有人已连接,以测试onOpen
方法是否正常工作。
public function onOpen(ConnectionInterface $conn) {
//store the new connection
$this->clients->attach($conn);
echo "someone connected\n";
}
Next is the onMessage
method. This method is called every time a message is sent by a specific client in the browser. The connection object of the client who sent the message, as well as the actual message, are passed along as an argument every time this method is called. All we do is loop through all the currently connected clients and send the message to them. In the code below, we’re checking if the client in the current iteration of the loop is the one who sent the message. We don’t want to send the same message to the person who sent it.
接下来是onMessage
方法。 每当浏览器中的特定客户端发送消息时,都会调用此方法。 每次调用此方法时,发送消息的客户端的连接对象以及实际消息都会作为参数传递。 我们要做的就是遍历所有当前连接的客户端并将消息发送给它们。 在下面的代码中,我们正在检查循环的当前迭代中的客户端是否是发送消息的客户端。 我们不想