Home > PHP Framework > Swoole > body text

How to use Swoole to implement WebSocket server and database interaction

WBOY
Release: 2023-11-07 09:32:28
Original
1338 people have browsed it

How to use Swoole to implement WebSocket server and database interaction

How to use Swoole to implement WebSocket server and database interaction

Introduction:
WebSocket is a full-duplex communication protocol based on the TCP protocol, which can be used on the client Establish real-time two-way communication with the server. Swoole is a PHP extension that can easily implement high-performance asynchronous and concurrent programming. In this article, we will introduce in detail how to use Swoole to build a WebSocket server and interact with the database.

Step 1: Install the Swoole extension

First, we need to install the Swoole extension. It can be installed through the following command:

pecl install swoole
Copy after login

Step 2: Create a WebSocket server

Next, we create a WebSocket server, listen to the specified port, and establish a connection with the client. This can be achieved using the following code:

<?php
$server = new SwooleWebSocketServer("0.0.0.0", 9501);

$server->on('open', function (SwooleWebSocketServer $server, $request) {
    echo "new connection open: {$request->fd}
";
});

$server->on('message', function (SwooleWebSocketServer $server, $frame) {
    echo "received message: {$frame->data}
";
    
    // 处理数据库交互
    $db = new mysqli('localhost', 'username', 'password', 'database');
    $result = $db->query("SELECT * FROM users");
    while ($row = $result->fetch_assoc()) {
        $server->push($frame->fd, json_encode($row));
    }
    $db->close();
});

$server->on('close', function ($ser, $fd) {
    echo "connection close: {$fd}
";
});

$server->start();
?>
Copy after login

In the above code, we create a WebSocket server and add three event callback functions. When a connection is established, the open event will be triggered; when a message is received, the message event will be triggered; when the connection is closed, the close event will be is triggered. In the message event, we can perform database interactive operations.

Step 3: Interact with the database

In the message event callback function, we use the MySQLi extension to interact with the database. First, we create a $db object and connect to the database using the constructor of mysqli. Next, we executed a query statement and sent the query results to the client through a while loop. Finally, we close the database connection.

It should be noted that for security reasons, we should set the relevant information of the database (such as user name and password) as environment variables and obtain these values ​​through the getenv() function, to avoid direct exposure to code.

Step 4: Run the server

Finally, we use the command line to run the above code. Switch to the directory where the code is located on the command line and execute the following command:

php server.php
Copy after login

If everything goes well, you will see that the WebSocket server starts successfully and is waiting for the client to connect. When a client connects to the server, the open event will be triggered and the connection ID will be displayed on the command line. When a message from the client is received, the message event will be triggered and the received message will be displayed on the command line. When the connection is closed, the close event will be triggered, and the closure of the connection will also be displayed on the command line.

Summary:
This article details how to use Swoole to implement the interaction between the WebSocket server and the database. By creating a WebSocket server and performing database interaction in the message event callback function, we can achieve real-time two-way communication. Using Swoole extensions, we can easily perform high-performance asynchronous and concurrent programming. I hope this article will be helpful to you in using Swoole to interact with the database.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template