Home > PHP Framework > Workerman > body text

How workerman implements simple barrage

Release: 2021-02-01 12:06:41
forward
3402 people have browsed it

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.

How workerman implements simple barrage

Related recommendations: "workerman Tutorial"

php code:

<?php  
use Workerman\Worker;  
require_once &#39;../Autoloader.php&#39;;//注意 这里要看你的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 = &#39;handle_connection&#39;;  
$text_worker->onMessage = &#39;handle_message&#39;;  
$text_worker->onClose = &#39;handle_close&#39;;  
  
Worker::runAll();
Copy after login

HTML code:

<!DOCTYPE html>  

<html>  
<head>  
    <meta charset="UTF-8">  
    <title>Simple Chat</title>  
</head>  
<body>  
    <center> 
<h1>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(&#39;raid&#39;);  
        };  
        ws.onmessage = function (e) {  
            document.getElementById("content").innerHTML += &#39;<marquee behavior="" direction="">&#39; + e.data + &#39;</marquee>&#39;;  
        };  
    };  
</script>  
  
</html>
Copy after login

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!

Related labels:
source:csdn.net
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!