workerman實(shí)現(xiàn)簡(jiǎn)單彈幕的方法

彈幕[dàn mù] (barrage),中文流行詞語(yǔ),指的是在網(wǎng)絡(luò)上觀看視頻時(shí)彈出的評(píng)論性字幕。下面我們就來(lái)看一下使用workerman實(shí)現(xiàn)簡(jiǎn)單彈幕的方法。

workerman實(shí)現(xiàn)簡(jiǎn)單彈幕的方法

相關(guān)推薦:《workerman教程

php代碼:

<?php    use workermanWorker;   require_once &#39;../Autoloader.php&#39;;//注意 這里要看你的workerman里的這個(gè)文件在哪 然后在進(jìn)行修改      $global_uid = 0;      // 當(dāng)客戶端連上來(lái)時(shí)分配uid,并保存連接,并通知所有客戶端   function handle_connection($connection) {       global $text_worker, $global_uid;       // 為這個(gè)鏈接分配一個(gè)uid       $connection->uid?=?++$global_uid;?? ????foreach?($text_worker-&gt;connections?as?$conn)?{?? ????????$conn-&gt;send("user[{$connection-&gt;uid}]?online");?? ????}?? }?? ?? //?當(dāng)客戶端發(fā)送消息過(guò)來(lái)時(shí),轉(zhuǎn)發(fā)給所有人?? function?handle_message($connection,?$data)?{?? ????global?$text_worker;?? ????foreach?($text_worker-&gt;connections?as?$conn)?{?? ????????$conn-&gt;send("user[{$connection-&gt;uid}]?said:?$data");?? ????}?? }?? ?? //?當(dāng)客戶端斷開(kāi)時(shí),廣播給所有客戶端?? 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>?? ?? 

本文轉(zhuǎn)自:https://blog.csdn.net/woshiyangyunlong/article/details/80174653

更多workerman知識(shí)請(qǐng)關(guān)注PHP中文網(wǎng)workerman教程教程欄目。

以上就是

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊9 分享
站長(zhǎng)的頭像-小浪學(xué)習(xí)網(wǎng)月度會(huì)員