Home  >  Article  >  Backend Development  >  Implement WebSocket service using PHP

Implement WebSocket service using PHP

WBOY
WBOYOriginal
2023-06-22 11:41:247100browse

With the development of the Internet, WebSocket technology is increasingly widely used. WebSocket technology can realize two-way real-time communication and is suitable for some application scenarios such as instant messaging and games, and can provide a better user experience. This article will introduce how to implement WebSocket service using PHP.

1. Introduction to WebSocket
WebSocket is a new protocol proposed by HTML5. It is a protocol for full-duplex communication on a single TCP connection. It supports real-time communication and has lower latency than HTTP. , which can carry out large amounts of data transmission. Using WebSocket can achieve real-time communication while reducing server overhead and saving network bandwidth.

2. WebSocket implementation method

  1. Server

(1)Create WebSocket server:

// 创建WebSocket服务器
$server = new swoole_websocket_server("0.0.0.0", 9502);

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

// 监听WebSocket消息事件
$server->on('message', function (swoole_websocket_server $server, $frame) {
    // 处理接收到的消息
    $message = "server received message: {$frame->data}
";
    // 向WebSocket客户端发送消息
    $server->push($frame->fd, $message);
});

// 监听WebSocket关闭事件
$server->on('close', function (swoole_websocket_server $server, $fd) {
    echo "client {$fd} closed
";
});

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

(2)Server Push message:

// 向WebSocket客户端发送消息
$server->push($frame->fd, $message);
  1. Client

(1) Create WebSocket connection:

// 创建WebSocket连接
var ws = new WebSocket("ws://localhost:9502");

// 监听WebSocket连接事件
ws.onopen = function() {
    console.log("WebSocket连接已打开");
};

// 监听WebSocket消息事件
ws.onmessage = function(event) {
    console.log("WebSocket收到消息:" + event.data);
};

// 监听WebSocket关闭事件
ws.onclose = function() {
    console.log("WebSocket连接已关闭");
};

(2) Client sends message:

// 向WebSocket服务器发送消息
ws.send("hello");

3. Detailed explanation of the implementation process

Below we will introduce in detail each part of the above code block.

  1. Server

(1) Create a WebSocket server:

// 创建WebSocket服务器
$server = new swoole_websocket_server("0.0.0.0", 9502);

This code creates a WebSocket server, specifying the IP address as 0.0.0.0 , the port is 9502.

(2) Listen to WebSocket connection events:

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

When a WebSocket client is connected, the 'open' event will be triggered. $request here is an object that contains some information about the client connection, such as the client's IP address, connection time, etc.

(3) Listen to WebSocket message events:

// 监听WebSocket消息事件
$server->on('message', function (swoole_websocket_server $server, $frame) {
    // 处理接收到的消息
    $message = "server received message: {$frame->data}
";
    // 向WebSocket客户端发送消息
    $server->push($frame->fd, $message);
});

When the WebSocket client sends a message, the 'message' event will be triggered. The $frame here is an object that contains some information about the received message, such as the content of the message, sending time, etc.

(4) Listen to the WebSocket closing event:

// 监听WebSocket关闭事件
$server->on('close', function (swoole_websocket_server $server, $fd) {
    echo "client {$fd} closed
";
});

When the WebSocket client closes the connection, the 'close' event will be triggered. $fd here is the unique identifier of the client connection.

(5) Start the WebSocket server:

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

Use the start() method to start the WebSocket server so that it can start monitoring client connections and messages.

(6) Server push message:

// 向WebSocket客户端发送消息
$server->push($frame->fd, $message);

Use the push() method to send a message to the specified client. $frame->fd is the unique identifier of the client connection, and $message is the content of the message to be sent.

  1. Client

(1) Create a WebSocket connection:

// 创建WebSocket连接
var ws = new WebSocket("ws://localhost:9502");

This code creates a WebSocket connection, specifying the URL as ws:// localhost:9502. Among them, ws represents the protocol type, localhost represents the IP address of the WebSocket server, and 9502 represents the listening port number.

(2) Listen to WebSocket connection events:

// 监听WebSocket连接事件
ws.onopen = function() {
    console.log("WebSocket连接已打开");
};

When the WebSocket connection is successful, the 'open' event will be triggered. Here we use the console.log() method to output a message.

(3) Listen to WebSocket message events:

// 监听WebSocket消息事件
ws.onmessage = function(event) {
    console.log("WebSocket收到消息:" + event.data);
};

When the WebSocket client receives a message, the 'message' event will be triggered. Here we use the console.log() method to output a message.

(4) Listen to the WebSocket closing event:

// 监听WebSocket关闭事件
ws.onclose = function() {
    console.log("WebSocket连接已关闭");
};

When the WebSocket connection is closed, the 'close' event will be triggered. Here we use the console.log() method to output a message.

(5) The client sends a message:

// 向WebSocket服务器发送消息
ws.send("hello");

Use the send() method to send a message to the WebSocket server. Here we send a string "hello".

4. Summary

This article introduces the method of using PHP to implement WebSocket service. WebSocket technology is widely used in real-time communications, games and other fields. It has lower latency than HTTP and can provide a better user experience. Using PHP to implement WebSocket services can quickly and easily achieve real-time communication, reduce server overhead, and save network bandwidth.

The above is the detailed content of Implement WebSocket service using PHP. 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