zoukankan      html  css  js  c++  java
  • tp5.1安装和使用workerman

    一、tp5.1安装workerman

    composer安装:

    composer require topthink/think-worker=2.0.*
    

    二、使用

    使用自定义类作为worker入口服务类

    在config中创建worker_server.php

    <?php
    
    // +----------------------------------------------------------------------
    // | Workerman设置 仅对 php think worker:server 指令有效
    // +----------------------------------------------------------------------
    return [
        'worker_class'   => 'app\index\controller\Worker', // 自定义Workerman服务类名 支持数组定义多个服务
    ];
    

    控制器自定义服务类Worker.php

    <?php
    
    namespace app\index\controller;
    
    use GatewayWorker\BusinessWorker;
    use GatewayWorker\Register;
    use GatewayWorker\Gateway;
    
    /**
     * 初始化gatewayWorker进程
     * Class Worker
     * @package app\index\controller
     */
    class Worker
    {
        public function __construct()
        {
            //初始化register进程
            new Register('text://0.0.0.0:1238');
    
            //初始化businessWorker进程
            $worker = new BusinessWorker();
            $worker->name = 'tuoyuyun';
            $worker->count = 4;
            $worker->registerAddress = '127.0.0.1:1238';
            $worker->eventHandler = 'app\index\controller\Events';//通信接收和发送类
    
            //初始化gateway进程
            $gateway = new Gateway('tcp://0.0.0.0:32769');//硬件通信ip和开放端口
            $gateway->name = 'gateway';
            $gateway->count = 4;
            $gateway->lanIp = '127.0.0.1';
            $gateway->startPort = 2900;
            $gateway->registerAddress = '127.0.0.1:1238';
        }
    }
    

    Events.php

    <?php
    
    
    namespace app\index\controller;
    
    use Exception;
    use \GatewayWorker\Lib\Gateway;
    
    class Events
    {
        /**
         * 当客户端连接时触发
         * 如果业务不需此回调可以删除onConnect
         *
         * @param int $client_id 连接id
         * @throws Exception
         */
        public static function onConnect($client_id)
        {
            Gateway::sendToClient($client_id,'success');
        }
    
        /**
         * 当客户端发来消息时触发
         * @param int $client_id 连接id
         * @param mixed $message 具体消息
         * @throws Exception
         */
        public static function onMessage($client_id, $message)
        {
            //自定义业务处理
            $index = new Index();
            $porson = $index->onMessage($client_id,$message);
            $res = Gateway::isOnline($client_id);
            if ($res){
                Gateway::sendToClient($client_id,$porson);
            }
        }
    
        /**
         * 当用户断开连接时触发
         * @param int $client_id 连接id
         * @throws Exception
         */
        public static function onClose($client_id)
        {
            Business::setState($client_id);
            // 向所有人发送
            GateWay::sendToAll("$client_id logout\r\n");
        }
    }
    
  • 相关阅读:
    58:二叉树的下一个节点
    57:删除链表中重复的结点
    56:链表中环的入口结点
    55:字符流中第一个不重复的字符
    54:表示数值的字符串
    53:正则表达式匹配
    52:构建成绩数组
    51:数组中重复的数字
    每个努力奋斗过的人,被不公正的际遇砸了满头包的时候,都有那么一瞬间的代入感。出生就是hard模式的人,早已经历了太多的劳其筋骨饿其体肤,再多的人为考验只会摧毁人对美好的向往。
    ClientValidationEnabled
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14424237.html
Copyright © 2011-2022 走看看