Home >PHP Framework >Workerman >Practical application of Workerman: creating a high-performance online chat room
Workerman Practical Application: Creating a High-Performance Online Chat Room
Introduction:
In today's Internet era, real-time online chat has become an indispensable part of people's lives. In order to meet users' needs for high-performance, real-time interaction, it becomes crucial to choose an appropriate communication framework. As a high-performance PHP asynchronous network communication framework, Workerman can meet this demand. This article will introduce how to use Workerman to build a high-performance online chat room and provide code examples.
1. Environment preparation
Before you start, make sure your environment meets the following conditions:
2. Create a chat room server
First, we need to create a chat room server. Create a file named "chat_server.php" and add the following code:
<?php require_once __DIR__ . '/vendor/autoload.php'; use WorkermanWorker; $ws_worker = new Worker("websocket://0.0.0.0:8000"); $ws_worker->count = 4; // 设置启动4个进程 $ws_worker->onConnect = function ($connection) { echo "有新用户连接 "; }; $ws_worker->onMessage = function ($connection, $data) use ($ws_worker) { foreach ($ws_worker->connections as $clientConnection) { $clientConnection->send($data); // 将收到的消息发送给所有连接的客户端 } }; $ws_worker->onClose = function ($connection) { echo "有用户断开连接 "; }; Worker::runAll();
Through the above code, we created a WebSocket server and listened to port 8000. When a new user connects, "New user connects" is output to the background. When a user disconnects, "A user disconnects" is output to the background. In the onMessage callback function, we send the received message to all connected clients.
3. Create a chat room client
Next, we need to create a simple chat room client. Create a file named "chat_client.html" and add the following code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>在线聊天室</title> <style> #message { height: 500px; border: 1px solid #ccc; padding: 10px; overflow-y: scroll; } #message p { margin: 5px 0; } #input { margin-top: 10px; } </style> </head> <body> <div id="message"></div> <input type="text" id="input" placeholder="请输入消息"> <button onclick="send()">发送</button> <script> var ws = new WebSocket("ws://localhost:8000"); ws.onopen = function () { console.log("连接成功"); }; ws.onmessage = function (evt) { var message = document.getElementById("message"); message.innerHTML += "<p>" + evt.data + "</p>"; message.scrollTop = message.scrollHeight; // 滚动到底部 }; ws.onclose = function () { console.log("连接关闭"); }; function send() { var input = document.getElementById("input"); var message = input.value; ws.send(message); input.value = ""; } </script> </body> </html>
With the above code, we have created a simple chat room client interface. When the user enters a message and clicks the send button, the message is sent to the server. When the server receives a message, the message is displayed in the message area in the onmessage callback function and automatically scrolls to the bottom.
4. Test the chat room
Execute the following command in the command line to start the server:
php chat_server.php start
Then, open two or more browser windows and visit "chat_client. html" file. Enter the message in the input box of different browser windows, click the send button, and you will see the same message in all windows. In this way we successfully created a high-performance online chat room.
Summary:
This article introduces how to use Workerman to build a high-performance online chat room, and provides corresponding code examples. By using Workerman, we can easily implement high-concurrency, low-latency real-time chat functions. I hope this article will be helpful to the practical application of Workerman.
The above is the detailed content of Practical application of Workerman: creating a high-performance online chat room. For more information, please follow other related articles on the PHP Chinese website!