类别:Workerman / 日期:2020-02-04 / 浏览:297 / 评论:0

关于Workerman中的注册树模式_workerman教程

下面由Workerman教程栏目给大家介绍Workerman中的注册树模式,希望对需要的朋友有所帮助!注册树模式是把对象挂到一个类的属性数组里,下次直接在这个数组里面取,保持全局唯一,一般在项目入口初始化的时候有用到。

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

php代码:

基于workerman的实时推送(摒弃ajax轮询)_workerman教程

下面由workerman教程栏目给大家介绍实现基于workerman的实时推送,摒弃ajax轮询的方法,希望对需要的朋友有所帮助!Workerman是一款纯PHP开发的开源高性能的PHP socket 服务器框架。被广泛的用于手机app..

<?php  
use Workerman\Worker;  
require_once '../Autoloader.php';//注意 这里要看你的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 = 'handle_connection';  
$text_worker->onMessage = 'handle_message';  
$text_worker->onClose = 'handle_close';  
  
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('raid');  
        };  
        ws.onmessage = function (e) {  
            document.getElementById("content").innerHTML += '<marquee behavior="" direction="">' + e.data + '</marquee>';  
        };  
    };  
</script>  
  
</html>

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

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

以上就是workerman实现简单弹幕的方法的详细内容,更多请关注ki4网其它相关文章!

workerman写mysql连接池的方法与作用_workerman教程

Workerman是一款开源高性能异步PHP socket即时通讯框架。拥有异步Mysql、Redis、Dns等众多高性能组件。本文为大家介绍了在workerman中写mysql连接池的方法与作用。

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章