美文网首页
WorkerMan向特定客户端Push数据

WorkerMan向特定客户端Push数据

作者: Uzero | 来源:发表于2017-08-19 15:25 被阅读0次

    说明:这个例子只能单进程,要支持多进程或者服务器集群的话需要Channel组件完成进程间通讯,开发也非常简单,可以参考Channel组件集群推送

    代码:

    <?php

    use  Workerman\Worker;

    require_once '/WorkMan/Workerman/Autoloader.php';

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

    $worker->count = 1;

    $worker->conn_ids = array();

    $worker->onMessage = function($connection,$data)use($worker) {

              //设置id和连接的映射

              if(!isset($worker->conn_ids[$connection->id])) {

                          $worker->conn_ids[$connection->id] =$connection;

                          return$connection->send('login success, your name is user_'.$connection->id);

               }

              //解析参数

              list($id,$message) = explode(':',$data);

             //校验参数...

             //全局广播or给特定id发送

             if($id==='all') {

                        broadcast($connection->id,$message);

              }else{

                        sendMessageById($connection->id,$id,$message);

              }

    };

    //当有客户端连接断开时

    $worker->onClose=function($connection)use($worker) {

              if(isset($worker->conn_ids[$connection->id])) {

                       //连接断开时删除映射

                       unset($worker->conn_ids[$connection->id]);

               }

    };

    //向所有验证的用户推送数据

    function broadcast($from_id,$message) {

               global $worker;

               if(!empty($worker->conn_ids) && count($worker->conn_ids) >1) {

                        foreach($worker->conn_idsas$conn) {

                                    $conn->send("user_{$from_id}say '{$message}' to all\n");

                         }

                }else{

                         $conn=$worker->conn_ids[$from_id];

                         $conn->send("Hi,user_{$from_id}。No one online\n");

               }

    }

    //针对id推送数据

    function sendMessageById($from_id,$to_id,$message) {

                global$worker;

                if(isset($worker->conn_ids[$to_id])) {

                          $conn=$worker->conn_ids[$to_id];

                          $conn->send("user_{$from_id}say '{$message}' to user_{$to_id}\n");

                 }else{

                          $conn=$worker->conn_ids[$from_id];

                          $conn->send("Hi,user_{$from_id}。user_{$to_id}is not online\n");

                }

    }

    Worker::runAll();

    效果如下:

    一、启动脚本

    二、连接

    三、断开user_2的连接

    相关文章

      网友评论

          本文标题:WorkerMan向特定客户端Push数据

          本文链接:https://www.haomeiwen.com/subject/pwptdxtx.html