Barrage [dàn mù] (barrage), a popular Chinese word, refers to the commentary subtitles that pop up when watching videos on the Internet. Let’s take a look at how to use Workerman to implement simple barrage.
Related recommendations: "workerman Tutorial"
php code:
<?php use Workerman\Worker; require_once '../Autoloader.php';//注意 这里要看你的workerman里的这个文件在哪 然后在进行修改 $global_uid = 0; // 当客户端连上来时分配uid,并保存连接,并通知所有客户端 function handle_connection($connection) { global $text_worker, $global_uid; // 为这个链接分配一个uid $connection->uid = ++$global_uid; foreach ($text_worker->connections as $conn) { $conn->send("user[{$connection->uid}] online"); } } // 当客户端发送消息过来时,转发给所有人 function handle_message($connection, $data) { global $text_worker; foreach ($text_worker->connections as $conn) { $conn->send("user[{$connection->uid}] said: $data"); } } // 当客户端断开时,广播给所有客户端 function handle_close($connection) { global $text_worker; foreach ($text_worker->connections as $conn) { $conn->send("user[{$connection->uid}] logout"); } } $text_worker = new Worker("websocket://0.0.0.0:2347"); $text_worker->count = 1; $text_worker->onConnect = 'handle_connection'; $text_worker->onMessage = 'handle_message'; $text_worker->onClose = 'handle_close'; Worker::runAll();
HTML code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Simple Chat</title> </head> <body> <center> <h1 id="Simple-nbsp-Chat">Simple Chat</h1> <input type="text" id="msg"> <button type="button" id="send">send</button> <div id="content" style="width:200px;height:200px;border:1px solid red"> 假装在播放视频 <marquee behavior="" direction=""></marquee> </div> </center> </body> <script type="text/javascript"> window.onload = function () { var ws = new WebSocket("ws://127.0.0.1:2347"); document.getElementById("send").onclick = function () { var msg = document.getElementById("msg").value; ws.send(msg); }; ws.onopen = function () { console.log("连接成功"); // ws.send('raid'); }; ws.onmessage = function (e) { document.getElementById("content").innerHTML += '<marquee behavior="" direction="">' + e.data + '</marquee>'; }; }; </script> </html>
This article is reproduced from: https://blog.csdn.net/woshiyangyunlong/article/details/80174653
For more workerman knowledge, please pay attention to the PHP Chinese websiteworkerman framework Tutorial section.
The above is the detailed content of How workerman implements simple barrage. For more information, please follow other related articles on the PHP Chinese website!

Workerman's WebSocket client enhances real-time communication with features like asynchronous communication, high performance, scalability, and security, easily integrating with existing systems.

The article discusses using Workerman, a high-performance PHP server, to build real-time collaboration tools. It covers installation, server setup, real-time feature implementation, and integration with existing systems, emphasizing Workerman's key f

The article discusses optimizing Workerman for low-latency applications, focusing on asynchronous programming, network configuration, resource management, data transfer minimization, load balancing, and regular updates.

The article discusses implementing real-time data synchronization using Workerman and MySQL, focusing on setup, best practices, ensuring data consistency, and addressing common challenges.

The article discusses integrating Workerman into serverless architectures, focusing on scalability, statelessness, cold starts, resource management, and integration complexity. Workerman enhances performance through high concurrency, reduced cold sta

The article discusses building a high-performance e-commerce platform using Workerman, focusing on its features like WebSocket support and scalability to enhance real-time interactions and efficiency.

Workerman's WebSocket server enhances real-time communication with features like scalability, low latency, and security measures against common threats.

The article discusses using Workerman, a high-performance PHP server, to build real-time analytics dashboards. It covers installation, server setup, data processing, and frontend integration with frameworks like React, Vue.js, and Angular. Key featur


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft