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");
        }
    }
    
  • 相关阅读:
    如何启动SOLR特性: 按层面检索
    solr的范围查询 TO
    jetty
    solr高亮的使用
    SQL日期加一天
    SQL从第二条开始取记录
    写出昨天的日期
    SQL取前后一条数据
    项目组【网站】的项目
    获取input文本框中高亮显示的值
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14424237.html
Copyright © 2011-2022 走看看