Home >PHP Framework >Swoole >How to use Swoole to implement WebSocket communication

How to use Swoole to implement WebSocket communication

王林
王林Original
2023-11-07 12:56:15903browse

How to use Swoole to implement WebSocket communication

Swoole is a high-performance PHP coroutine network framework that supports asynchronous IO, multi-process, multi-thread, coroutine and other features. Among them, the WebSocket component provided by Swoole can be used to achieve real-time two-way communication and is an ideal choice for building real-time applications. This article will introduce how to use Swoole to implement WebSocket communication and provide specific code examples.

1. Environment preparation

Before using Swoole to implement WebSocket communication, you need to ensure that the Swoole extension has been installed. It can be installed through the following command:

pecl install swoole

or download the source code build from the official GitHub repository.

2. Create a WebSocket server

Introduce Swoole's WebSocket component into the code, create a WebSocket server, and monitor the connection with the client. The code is as follows:

use SwooleWebSocketServer;

// 创建WebSocket服务器
$server = new Server('0.0.0.0', 9501);

// 监听WebSocket连接事件
$server->on('open', function (Server $server, $request) {
    echo "Client {$request->fd} connected
";
});

// 启动服务器
$server->start();

The above code creates a WebSocket server with a listening port of 9501, and prints the file descriptor (fd) of the connected client when the connection is established.

3. Processing WebSocket messages

When the WebSocket server establishes a connection with the client, the client can send messages to the server. The server needs to listen for message events with the client and process them. The process of processing WebSocket messages is similar to that of HTTP requests. The message content can be obtained by parsing the message header and obtaining the message body. The code is as follows:

// 监听WebSocket消息事件
$server->on('message', function (Server $server, $frame) {
    echo "Received message: {$frame->data}
";
});

The above code listens to the WebSocket message event and prints the message content when the message is received.

4. Sending a message to the WebSocket client

To send a message to the client in the WebSocket server, you need to use the server's push method. This method accepts the client's file descriptor and the content of the message that needs to be sent. The code is as follows:

// 监听WebSocket消息事件
$server->on('message', function (Server $server, $frame) {
    echo "Received message: {$frame->data}
";
    
    // 向客户端发送消息
    $server->push($frame->fd, 'Server received message: '.$frame->data);
});

The above code replies a message to the client when processing WebSocket messages.

5. Complete code example

use SwooleWebSocketServer;

// 创建WebSocket服务器
$server = new Server('0.0.0.0', 9501);

// 监听WebSocket连接事件
$server->on('open', function (Server $server, $request) {
    echo "Client {$request->fd} connected
";
});

// 监听WebSocket消息事件
$server->on('message', function (Server $server, $frame) {
    echo "Received message: {$frame->data}
";
    
    // 向客户端发送消息
    $server->push($frame->fd, 'Server received message: '.$frame->data);
});

// 启动服务器
$server->start();

6. WebSocket client

After completing the construction of the WebSocket server, we need to use the WebSocket client to send messages to the server and receive messages from the server reply. The following is a sample code for a WebSocket client:

// 创建WebSocket连接
const ws = new WebSocket('ws://localhost:9501');

// 监听WebSocket连接事件
ws.addEventListener('open', function (event) {
    console.log('Connected to WebSocket server');
    
    // 发送消息
    ws.send('Hello, Swoole WebSocket');
});

// 监听WebSocket消息事件
ws.addEventListener('message', function (event) {
    console.log('Received message:', event.data);
});

The above code uses JavaScript to create a WebSocket connection and sends a message to the WebSocket server after the connection is established. When the message is processed by the server, the server will send a reply message back to the client, and the client can receive the reply message by listening to the message event.

7. Summary

This article introduces how to use Swoole to implement WebSocket communication, and shows through code examples how to create a WebSocket server, process messages, and send messages to the client. Using Swoole's WebSocket component you can easily build real-time two-way communication applications.

The above is the detailed content of How to use Swoole to implement WebSocket communication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn