PHP WebSocket development examples: practical cases of how to implement specific functions
Introduction:
With the development of the Internet, the traditional HTTP protocol exists in real-time communication Some restrictions. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time communication between the client and the server. This article will use a practical case to introduce how to use PHP WebSocket to implement specific functions.
(1) Establish a WebSocket server
First, we need to build a WebSocket server. You can use PHP's swoole extension to build a WebSocket server. Swoole is a high-performance PHP extension that provides support for WebSocket. By executing the following code, we can create a WebSocket server:
<?php $server = new swoole_websocket_server("0.0.0.0", 9501); $server->on('open', function ($server, $request) { echo "new connection open: ".$request->fd; }); $server->on('message', function ($server, $frame) { echo "received message: ".$frame->data; }); $server->on('close', function ($ser, $fd) { echo "connection close: ".$fd; }); $server->start(); ?>
In this code, we create a WebSocket server with a listening IP address of 0.0.0.0 and a port number of 9501. When a new connection is established, the server will automatically call the open
event processing function; when receiving a message from the client, the server will call the message
event processing function; and when When the connection is closed, the server will call the close
event handling function.
(2) Front-end page development
Next, we need to develop a front-end page to display the chat room interface and communicate with the server. The following is a simple front-end page code:
<!DOCTYPE html> <html> <head> <title>WebSocket Chat Room</title> <script> var socket = new WebSocket("ws://localhost:9501"); socket.onopen = function(event) { console.log("WebSocket connection open"); }; socket.onmessage = function(event) { console.log("Received message: " + event.data); }; socket.onclose = function(event) { console.log("WebSocket connection closed"); }; </script> </head> <body> <h1>WebSocket Chat Room</h1> <div id="chat-messages"></div> <input type="text" id="input-message"> <button onclick="sendMessage()">Send</button> <script> function sendMessage() { var message = document.getElementById("input-message").value; socket.send(message); } </script> </body> </html>
This code connects to the server through WebSocket and defines the processing functions for opening the connection, receiving messages, and closing the connection. Through buttons and text boxes, users can enter chat content and send it to the server.
(3) Chat room function implementation
On the server side, we need to add some logic to implement the chat room function. Modify the server code as follows:
<?php $server = new swoole_websocket_server("0.0.0.0", 9501); $server->on('open', function ($server, $request) { echo "new connection open: ".$request->fd." "; }); $server->on('message', function ($server, $frame) { echo "received message: ".$frame->data." "; // 广播消息给所有客户端 foreach ($server->connections as $fd) { $server->push($fd, $frame->data); } }); $server->on('close', function ($ser, $fd) { echo "connection close: ".$fd." "; }); $server->start(); ?>
In the modified code, when receiving a message from the client, the server will broadcast the message to all connected clients. In this way, all messages sent by users on the chat room page will be displayed on other users' pages in real time.
Summary:
Through the above practical cases, we learned how to use PHP WebSocket to implement a simple online chat room. With the help of PHP's swoole extension, we can easily build a server based on WebSocket to achieve real-time communication functions. In addition, WebSocket can be used in more scenarios, such as real-time data display, multi-person collaborative editing, etc. I hope this article will be helpful in understanding and applying PHP WebSocket.
The above is the detailed content of PHP WebSocket development examples: practical cases of how to implement specific functions. For more information, please follow other related articles on the PHP Chinese website!