workerman's method to realize simple bullet curtain

Keywords: PHP Javascript

Barrage, a popular Chinese term, refers to the comment subtitles that pop up when watching vi d eos on the Internet. Let's take a look at how to use workerman to implement a simple barrage.

How to become an architect from a coder must see the knowledge point: Catalog Daquan (continuous update) 50W annual salary challenge! ​

php code:

<?php  

use Workerman\Worker;  

require_once '../Autoloader.php';//Note that it depends on where this file is in your workerman and is being modified  

   

$global_uid = 0;  

   

// Allocate uid when the client connects, save the connection, and notify all clients  

function handle_connection($connection) {  

    global $text_worker, $global_uid;  

    // Assign a uid to this link  

    $connection->uid = ++$global_uid;  

    foreach ($text_worker->connections as $conn) {  

        $conn->send("user[{$connection->uid}] online");  

    }  

}  

   

// When the client sends a message, it forwards it to everyone  

function handle_message($connection, $data) {  

    global $text_worker;  

    foreach ($text_worker->connections as $conn) {  

        $conn->send("user[{$connection->uid}] said: $data");  

    }  

}  

   

// Broadcast to all clients when the client is disconnected  

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>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">

    //Pretend to be playing video

    <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("Successful connection");  

//            ws.send('raid');  

        };  

        ws.onmessage = function (e) {  

            document.getElementById("content").innerHTML += '<marquee behavior="" direction="">' + e.data + '</marquee>';  

        };  

    };  

</script>  

   

</html>

  

These are the details of workerman's method of implementing simple bullet curtain

More at

How to become an architect from a coder must see the knowledge point: Catalog Daquan (continuous update) 50W annual salary challenge!

 

Posted by Boerboel649 on Tue, 17 Mar 2020 10:38:32 -0700