• 在thinkphp5.1使用 Workerman 进行 socket 通讯


    1.安装 Workerman
    安装GatewayWorker内核文件(不包含start_gateway.php start_businessworker.php等启动入口文件),直接上composer

    composer require workerman/gateway-worker

    2.创建 Workerman 启动文件
    创建一个自定义命令类文件来启动 Socket 服务端,新建

    application/common/command/Workerman.php
    <?php
    /**
     * User: Tegic
     * Date: 2018/6/13
     * Time: 09:36
     */
     
    namespace appcommoncommand;
     
    use appworkermanEvents;
    use GatewayWorkerBusinessWorker;
    use GatewayWorkerGateway;
    use GatewayWorkerRegister;
    use thinkconsoleCommand;
    use thinkconsoleInput;
    use thinkconsoleinputArgument;
    use thinkconsoleinputOption;
    use thinkconsoleOutput;
    use WorkermanWorker;
     
    class Workerman extends Command
    {
        protected function configure()
        {
            $this->setName('workerman')
                ->addArgument('action', Argument::OPTIONAL, "action  start|stop|restart")
                ->addArgument('type', Argument::OPTIONAL, "d -d")
                ->setDescription('workerman chat');
        }
        
        protected function execute(Input $input, Output $output)
        {
            global $argv;
            $action = trim($input->getArgument('action'));
            $type   = trim($input->getArgument('type')) ? '-d' : '';
            
            $argv[0] = 'chat';
            $argv[1] = $action;
            $argv[2] = $type ? '-d' : '';
            $this->start();
        }
        private function start()
        {
            $this->startGateWay();
            $this->startBusinessWorker();
            $this->startRegister();
            Worker::runAll();
        }
        
        private function startBusinessWorker()
        {
            $worker                  = new BusinessWorker();
            $worker->name            = 'BusinessWorker';
            $worker->count           = 1;
            $worker->registerAddress = '127.0.0.1:1236';
            $worker->eventHandler    = Events::class;
        }
        
        private function startGateWay()
        {
            $gateway = new Gateway("websocket://0.0.0.0:8282");
            $gateway->name                 = 'Gateway';
            $gateway->count                = 1;
            $gateway->lanIp                = '127.0.0.1';
            $gateway->startPort            = 2300;
            $gateway->pingInterval         = 30;
            $gateway->pingNotResponseLimit = 0;
            $gateway->pingData             = '{"type":"@heart@"}';
            $gateway->registerAddress      = '127.0.0.1:1236';
        }
        
        private function startRegister()
        {
            new Register('text://0.0.0.0:1236');
        }
    }

    配置 application/command.php 文件

    return [
        'appcommoncommandWorkerman',
    ];

    3.创建事件监听文件
    创建 application/workerman/Events.php 文件来监听处理 workerman 的各种事件

    <?php
    /**
     * User: Tegic
     * Date: 2018/6/13
     * Time: 09:47
     */
     
    namespace appworkerman;
     
    use GatewayWorkerLibGateway;
     
    class Events
    {
        
         public static function onWorkerStart($businessWorker)
        {
        }
     
        public static function onConnect($client_id)
        {
        }
     
        public static function onWebSocketConnect($client_id, $data)
        {
        }
     
        public static function onMessage($client_id, $message)
        {
        }
     
        public static function onClose($client_id)
        {
        }
    }

    4.启动 Workerman 服务端

    以debug(调试)方式启动

    php think workerman start

    以daemon(守护进程)方式启动

    php think workerman start d
     

    查看状态

    php think workerman status

    当你看到如下结果的时候,workerman已经启动成功了。

    ----------------------- WORKERMAN -----------------------------
    Workerman version:3.5.14          PHP version:7.0.30
    ------------------------ WORKERS -------------------------------
    user          worker                 listen                    processes status
    root          YourAppBusinessWorker  none                       4         [OK] 
    root          YourAppGateway         websocket://0.0.0.0:8382   4         [OK] 
    root          Register               text://0.0.0.0:1237        1         [OK] 
    ----------------------------------------------------------------
  • 相关阅读:
    每个部门都有自己的游戏规则
    ssh作为代理,反向登录没有固定公网ip的局域网内的某远程服务器
    x11vnc 作为远程桌面服务器时vnc客户端键盘无法长按连续输入字符
    vim 编译使用ycm启动问题 fixed
    ubuntu设置普通用户也能执行docker命令
    git常见使用
    切图的必要步骤
    css居中
    清除浮动
    Spring-AOP(2)
  • 原文地址:https://www.cnblogs.com/cainiaoaixuexi/p/13803460.html
Copyright © 2020-2023  润新知