workerman实现简单弹幕的方法

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

workerman实现简单弹幕的方法

相关推荐:《workerman教程

php代码:

<?php    use workermanWorker;   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-&gt;connections as $conn) {           $conn-&gt;send("user[{$connection-&gt;uid}] online");       }   }      // 当客户端发送消息过来时,转发给所有人   function handle_message($connection, $data) {       global $text_worker;       foreach ($text_worker-&gt;connections as $conn) {           $conn-&gt;send("user[{$connection-&gt;uid}] said: $data");       }   }      // 当客户端断开时,广播给所有客户端   function handle_close($connection) {       global $text_worker;       foreach ($text_worker-&gt;connections as $conn) {           $conn-&gt;send("user[{$connection-&gt;uid}] logout");       }   }      $text_worker = new Worker("websocket://0.0.0.0:2347");      $text_worker-&gt;count = 1;      $text_worker-&gt;onConnect = 'handle_connection';   $text_worker-&gt;onMessage = 'handle_message';   $text_worker-&gt;onClose = 'handle_close';      Worker::runAll();

html代码:

nbsp;html&gt;              <meta>       <title>Simple Chat</title>             <center>  <h1>Simple Chat</h1>   <input>   <button>send</button>    <div>     假装在播放视频     <marquee></marquee> </div>   </center>       <script>       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;&#39;;           };       };   </script>      

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

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

以上就是

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享