Getting started with PHP multi-threaded programming: Creating a WebSocket server using the swoole extension
Preface
In Web development, real-time communication has become an increasingly important requirement. The traditional HTTP protocol cannot meet the needs of real-time communication, and the WebSocket protocol has become the solution. In order to implement a WebSocket server in PHP, we can use the swoole extension to create a multi-threaded server.
1. What is swoole?
swoole is a PHP extension that provides support for asynchronous, multi-threading and coroutines. By using swoole, we can create concurrent servers, asynchronous tasks, long connections and high-performance network applications in PHP. It provides a series of APIs to implement these functions, including support for different protocols such as TCP, UDP, HTTP, WebSocket, etc.
2. Preparation
Before you start, you need to make sure you have installed the swoole extension. You can use the following command to install swoole in a Linux system:
pecl install swoole
Or in a Windows system, you can download the swoole binary package from the official website and extract it to the PHP extension directory. Then add the following configuration in the php.ini file:
extension=swoole
3. Create a WebSocket server
Below we will use a simple example to demonstrate how to use the swoole extension to create a WebSocket server.
First, we create a server.php file, introduce the swoole extension, and initialize a WebSocket server:
<?php $server = new SwooleWebSocketServer("0.0.0.0", 9501);
Then, we can set Some server parameters, such as the number of worker processes and listening ports:
$server->set([ 'worker_num' => 4, 'max_request' => 10000, ]);
Next, we listen to the open event of the WebSocket connection and process it accordingly:
$server->on('open', function ($server, $request) { echo "new connection: {$request->fd} "; });
Then, we listen to the WebSocket message event and process it accordingly:
$server->on('message', function ($server, $frame) { echo "received message: {$frame->data} "; // 可以在这里编写自定义的业务逻辑处理 });
Finally, we listen to the closing event of the WebSocket connection and process it accordingly:
$server->on('close', function ($server, $fd) { echo "connection closed: {$fd} "; });
Finally, we start the WebSocket server:
$server->start();
Run server.php in the command line:
php server.php
4. Test the WebSocket server
Now that we have created a WebSocket server, we can use a simple HTML page to test it.
First, create an index.html file and write the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebSocket Test</title> <script> var ws = new WebSocket("ws://localhost:9501"); ws.onopen = function() { console.log("WebSocket connection open."); }; ws.onmessage = function(evt) { console.log("received message: " + evt.data); }; ws.onclose = function() { console.log("WebSocket connection closed."); }; </script> </head> <body> <h1>WebSocket Test</h1> </body> </html>
Summary
By using the swoole extension, we can easily create a WebSocket server in PHP. In actual project development, we can further improve and optimize the functions of the WebSocket server according to specific needs. At the same time, swoole also provides more functions and APIs that can be used to handle concurrent, asynchronous and high-performance network applications, allowing for further in-depth learning and practice.
The above is the detailed content of Introduction to PHP multi-threaded programming: Create a WebSocket server using the swoole extension. For more information, please follow other related articles on the PHP Chinese website!