从零搭建聊天系统之Lumen+GatewayWorker

环境必备

1.composer已经安装
2.lumen5.5+
备注:涉及技术Nginx,SSL,PHP-FPM,Socket,MySQL,WSS,Redis,WorkerMan,Linux,七牛云SDK,Laravel,本专题都基于Mac系统,文档里会有大量的类Linux命令,该教程只适合类Linux下开发者试验ORLinux开发者

1.composer依赖

composer require workerman/workerman
composer require   workerman/gateway-worker

2.重定向事件处理类,方便结合Lumen与Controller,两种方法
1.修改BusinessWorker.php

/**
     * 事件处理类,默认是 Events 类
     *
     * @var string
     */
    public $eventHandler = 'App\Http\Controllers\WebSocket\WebSocketController';

2.OR在SocketSever类中实例化BusinessWorker后

$worker = new BusinessWorker();
$worker->eventHandler = 'App\Http\Controllers\WebSocket\WebSocketController';

3.创建事件处理类

 $client_id,
                'msgType' => 0,
            ],320));
    }

    public static function onMessage($client_id, $data)
    {
        if(!is_array($data)){
            $data = json_decode($data,true);
        }
        switch ($data['type']){
            case 0:
                //绑定uid,更改上线状态,通知该用户所有的好友
  
                break;
            case 1:
                //点对点消息
            
                break;
            case 4:
                //心跳 忽略
                break;
            case 5: //群组消息
              
                break;
            case 7:
                //客户端申请通过要求准备推送群组消息
              
            default :
         
        }
    }

    public static function sendUid($uid,$data){
        Gateway::sendToUid($uid,json_encode($data,320));
    }

    public static function sendAll($content = 'Test'){
        $data = [
            'username'=>"系统消息",
            'avatar'=>"http://www.qqpk.cn/Article/UploadFiles/201501/20150113145724330.jpg",
            'id'=>"10000",
            'type'=>"friend",
            'content'=>"这是来自系统的消息:".$content,
            'msgType' => 1
        ];
        Gateway::sendToAll(json_encode($data,320));
    }

    public static function onClose($client_id){
      
            }
        }
    }
}

4.通常,我们是以php artisan ***命令启动一个服务或者服务,那么我们将WorkerMan也结合一下,用php artisan work:socket来启动
先创建SocketServer服务类继续lumen的Command类

argument('action');
        $argv[0]='worker:socket';
        $argv[1]=$action;
        $argv[2]=$this->option('d')?'-d':'';

        $context = array(
            'ssl' => array(
                'local_cert'  => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.crt', // 或者crt文件
                'local_pk'    => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.key',
                'verify_peer' => false
            )
        );
        $Gateway = new Gateway('websocket://0.0.0.0:9006',$context);
        $Gateway->transport = 'ssl';

//非ssl加密        $Gateway = new Gateway('websocket://0.0.0.0:8082');

        new Register('text://0.0.0.0:1238');
        $Gateway->name = 'Gateway';
        $Gateway->count = 1;
        $Gateway->lanIp = '127.0.0.1';
        $Gateway->startPort = 10000;
        $Gateway->pingInterval = 10;  //10s一次心跳
        $Gateway->registerAddress = '127.0.0.1:1238';
        $Gateway->pingNotResponseLimit = 3;
        $Gateway->pingData = '';
        $worker = new BusinessWorker();
        $worker->eventHandler = 'App\Http\Controllers\WebSocket\WebSocketController';
        $worker->name = 'BusinessWorker';
        $worker->count = 3;
        $worker->registerAddress = '127.0.0.1:1238';
        Gateway::runAll();
    }
}


// 这个地方注意,一般都是非ssl协议实列化,如果你有证书就可以像我一样,修改crt和key的证书地址
 //$Gateway = new Gateway('websocket://0.0.0.0:8082');,

继续在Kernel.php文件中加入我们的刚刚添加的SocketServer类

现在我们来尝试让artisan 列举出所有的命令

php artisan list
从零搭建聊天系统之Lumen+GatewayWorker_第1张图片
WechatIMG368.jpeg

我们可以看到在命令中已经有了work:socket命令
现在我们来让他启动:


从零搭建聊天系统之Lumen+GatewayWorker_第2张图片
WechatIMG369.jpeg

你可能感兴趣的:(从零搭建聊天系统之Lumen+GatewayWorker)