ThinkPHP5.0 整合 GatewayWorker

日志 / 1035人浏览 / 0人评论
最近想用Tp5.0和GatewayWorker结合做项目,总结了一下流程

  1. 安装ThinkPHP5.0
  2. 在ThinkPHP5.0根目录里composer安装GatewayWorker,

    composer require workerman/gateway-worker

  3. 安装完成之后,可以在vendor目录下看到workerman, GatewayWorker就在workman目录里,因为GatewayWorker是基于workerman来开发的。

  4. 建立以下文件

    



  • 建立启动文件。在根目录建立server.php,内容如下:
    define('APP_PATH', __DIR__.'/application/');
    
    define('BIND_MODULE', 'index/Worker');
    
    // 加载框架引导文件
    require __DIR__.'/thinkphp/start.php';

  • 再创建application\push\controller\Run.php 文件,内容如下:
    namespace app\push\controller;
    
    use Workerman\Worker;
    use GatewayWorker\Register;
    use GatewayWorker\BusinessWorker;
    use GatewayWorker\Gateway;
    
    
    class Run 
    {
    
        public function __construct(){
            // 初始化register
            new Register('text://0.0.0.0:1236');
    
            //初始化 bussinessWorker 进程
            $worker = new BusinessWorker();
            $worker->name = 'RoomBusinessWorker';
            $worker->count = 4;
            $worker->registerAddress = '127.0.0.1:1236';
    
            // 设置处理业务的类,此处制定Events的命名空间
            $worker->eventHandler = '\app\push\controller\Events';
            // 初始化 gateway 进程
            $gateway = new Gateway("websocket://0.0.0.0:7272");
            $gateway->name = 'RoomGateway';
            $gateway->count = 4;
            $gateway->lanIp = '127.0.0.1'; 
            $gateway->startPort = 2300;
            $gateway->registerAddress = '127.0.0.1:1236';
    
            // 运行所有Worker;
            Worker::runAll();
        }
    
    

    }

  • 在Linux环境直接在根目录运行 php start.php 就可以了,在windows下会报错

  • 不允许创建多个workers,这时可以分开创建workers. 创建以下几个文件

  • application\push\controller\Sregister.php

    namespace app\push\controller;
    
    use Workerman\Worker;
    use GatewayWorker\Register;
    
    
    class Sregister
    {
        public function __construct(){
            $register = new Register('text://0.0.0.0:1236');
    
            Worker::runAll();
    
        }
    

    }



  • start_register.php (根目录下,用于启动Sregister.php)

    define('APP_PATH', __DIR__ . '/application/');
    define('BIND_MODULE','push/Sregister');
    

    require __DIR__.'/thinkphp/start.php';



  • application\push\controller\Sbusinessworker.php

    namespace app\push\controller;
    
    use think\Request;
    use Workerman\Worker;
    use GatewayWorker\BusinessWorker;
    use Workerman\Autoloader;
    class Sbusinessworker
    {
        public function __construct()
        {
            // bussinessWorker 进程
            $worker = new BusinessWorker();
            // worker名称
            $worker->name = 'ChatBusinessWorker';
            // bussinessWorker进程数量
            $worker->count = 4;
            // 服务注册地址
            $worker->registerAddress = '127.0.0.1:1236';
            //设置处理业务的类,此处制定Events的命名空间
            $worker->eventHandler = 'app\push\controller\Events';
    
            Worker::runAll();
        }
    

    }



  • start_businessworker.php (根目录下,用于启动Sbusinessworker.php)

    define('APP_PATH', __DIR__ . '/application/');
    define('BIND_MODULE','push/Sbusinessworker');
    

    require __DIR__.'/thinkphp/start.php';



  • application\push\controller\Gate.php
    
    namespace app\push\controller;
    
    use Workerman\Worker;
    use GatewayWorker\Gateway;
    use GatewayWorker\Register;
    use GatewayWorker\BusinessWorker;
    
    
    class Gate
    {
    
        public function __construct()
        {
            
            // 初始化 gateway 进程
            $gateway = new Gateway("websocket://0.0.0.0:7272");
            // 设置名称,方便status时查看
            $gateway->name = 'ChatGateway';
            $gateway->count = 4;
            // 分布式部署时请设置成内网ip(非127.0.0.1)
            $gateway->lanIp = '127.0.0.1';
            // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
            // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
            $gateway->startPort = 2300;
            // 心跳间隔
            $gateway->pingInterval = 10;
            // 心跳数据
            $gateway->pingData = '{"type":"ping"}';
            // 服务注册地址
            $gateway->registerAddress = '127.0.0.1:1236';
    
            //运行所有Worker;
            Worker::runAll();
        }
    

    }



  • start_gateway.php (根目录下,用于启动Gate.php)

    define('APP_PATH', __DIR__ . '/application/');
    define('BIND_MODULE','push/Gate');
    

    require __DIR__.'/thinkphp/start.php';



  • application\push\controller\Events.php (Gatway的连接、推送、接受、断开都在这个文件处理)

    namespace app\push\controller;
    
    use GatewayWorker\Lib\Gateway;
    
    
    class Events
    {
    
        /**
         * 有消息时
         * @param integer $client_id 连接的客户端
         * @param mixed $message
         * @return void
         */
        public static function onMessage($client_id, $message)
        {
            // debug
            echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']}  client_id:$client_id session:".json_encode($_SESSION)." onMessage:".$message."\n";
        }
    
        /**
         * 当用户连接时触发的方法
         * @param integer $client_id 连接的客户端
         * @return void
         */
        public static function onConnect($client_id)
        {
    
            
            Gateway::sendToClient($client_id, "Your client_id is $client_id");
        }
    
        /**
         * 当用户断开连接时触发的方法
         * @param integer $client_id 断开连接的客户端
         * @return void
         */
        public static function onClose($client_id)
        {
            Gateway::sendToAll("client[$client_id] logout\n");
        }
    
        /**
         * 当进程启动时
         * @param integer $businessWorker 进程实例
         */
        public static function onWorkerStart($businessWorker)
        {
            echo "WorkerStart\n";
        }
    
        /**
         * 当进程关闭时
         * @param integer $businessWorker 进程实例
         */
        public static function onWorkerStop($businessWorker)
        {
            echo "WorkerStop\n";
        }
    
    

    }


  • 在根目录下写一个脚本start_for_win.bat,

    php start_register.php start_gateway.php start_businessworker.php
        pause



  • 如果是在windows系统下,直接点击start_for_win.bat就可以启动GatewayWorker了

    所有的业务逻辑用thinkphp来处理,而需要向客户端主动推送消息时就需要调用Gateway的API(application\push\controller\Events.php)







































    




    








    

    

    




    

    



            











感谢博主,喝杯咖啡~

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我