zoukankan      html  css  js  c++  java
  • workerman channel组件集群推送

    <?php
    use WorkermanWorker;

    require_once '../../web/Workerman/Autoloader.php';
    require_once '../../web/Channel/src/Server.php';
    require_once '../../web/Channel/src/Client.php';

    $worker = new Worker('text://0.0.0.0:0623');

    $channel = new ChannelServer('0.0.0.0', 8888);

    $worker->count = 3;
    $worker->onWorkerStart = function ($worker)
    {
    ChannelClient::connect('127.0.0.1', 8888);
    ChannelClient::on('broadcast', function($event_data)use($worker){
    $event_data = json_decode($event_data);
    if (!empty($event_data->work_id) && !empty($event_data->to_connect_id)) {
    $worker_id = $event_data->work_id;
    $to_connect_id = $event_data->to_connect_id;
    if ($worker_id == $worker->id) {
    foreach ($worker->connections as $connection) {
    if ($connection->id == $to_connect_id) {
    $connection->send($event_data->content);
    }
    }
    }
    } else {
    foreach ($worker->connections as $connection) {
    $connection->send($event_data['content']);
    }
    }
    });
    };
    $worker->onConnect = function ($connection)use($worker)
    {
    echo '用户姐夫' . 'worker id ' . $worker->id . ',connection id ' . $connection->id . "上线 ";};$worker->onMessage = function ($connection, $data)use($worker){ echo '用户姐夫' . 'worker id ' . $worker->id . ',connection id ' . $connection->id . "上线 "; $event_data['content'] = $data; $event_data['work_id'] = 2; $event_data['to_connect_id'] = 2;//这里模拟只向work进程id为2 connect id 为2 的tcp链接发送信息 $data_event = json_encode($event_data); ChannelClient::publish('broadcast', $data_event);};Worker::runAll();
  • 相关阅读:
    TweenLite简单运用
    nodejs 重定向 (redirect + writeHead(Location))
    Nodejs Web模块( readFile 根据请求跳转到响应html )
    Express框架(http服务器 + 路由)
    AI 学习路线
    implicitly_wait()隐式等待
    Python 爬虫基础Selenium
    Selenium2+python自动化15-select下拉框
    python selenium while 循环
    jupyter notebook修改默认路径和浏览器
  • 原文地址:https://www.cnblogs.com/jasonxiaoqinde/p/7463343.html
Copyright © 2011-2022 走看看