搜索
首页php框架Workermanworkerman实现简单弹幕的方法

workerman实现简单弹幕的方法

Feb 03, 2020 pm 04:27 PM
workerman

弹幕[dàn mù] (barrage),中文流行词语,指的是在网络上观看视频时弹出的评论性字幕。下面我们就来看一下使用workerman实现简单弹幕的方法。

workerman实现简单弹幕的方法

相关推荐:《workerman教程

php代码:

<?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();

HTML代码:

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

本文转自:https://blog.csdn.net/woshiyangyunlong/article/details/80174653

更多workerman知识请关注PHP中文网workerman框架教程栏目。

以上是workerman实现简单弹幕的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:csdn。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具