swoole多端口监听

  今天测试swoole写webserver实现多端口监听。记录下爬过的坑:关于tcp协议监听触发不到receive!!!!!

  首先上服务端代码

class Http
{

    /**
     * 服务实例
     * @var Server
     */
    protected $server;


    public function run(){

        $setting = Config::get('http');
        /*开启监听*/
        $this->server = new Server($setting['host'],$setting['port']);

        if (isset($setting['config']) && $setting['config'])$this->server->set($setting['config']);


        if (isset($setting['tcp_check']) && $setting['tcp_check']){
            /*开启多端口监听*/
            $port = $this->server->addListener('127.0.0.1','9501',SWOOLE_SOCK_TCP);
            $port->on('connect',[$this,'connect']);
            $port->on('receive',[$this,'receive']);
        }

        $this->server->on('start',[$this,'start']);
        $this->server->on('request',[$this,'request']);

        $this->server->on('workerStart',[$this,'workerStart']);

        $this->server->start();
    }


    /**
     * 主进程启动
     * @param Server $server
     */
    public function start(Server $server){
        $reload = Reload::getInstance(Config::get('reload_watch'));
        /*启动定时热重启检测*/
        swoole_timer_tick(6000,function ()use ($reload){
            if ($reload->reload()) $this->server->reload();
        });
    }

    /**
     *
     * @param Request $request
     * @param Response $response
     */
    public function request(Request $request,Response $response){

        $route = Route::get_instance();
        $route->dispatch($request,$response);
    }



    public function workerStart(Server $server, int $worker_id){
        /*可热重启配置*/
        Config::loadLazy();
        /*加载路由*/
        include_once APP_PATH.'/route.php';
    }

    public function connect(Server $server,int $fd, int $reactorId){
        var_dump('connection');
    }

    public function receive(Server $server, int $fd, int $reactor_id, string $data){
        var_dump('receive',$data);
    }


}

  当我调用本地tcp client向服务发送tcp请求并发送数据的时候发现如下结果:

[root@bin]# php walter.php 
string(10) "connection"

  发现没,tcp监听服务只触发了connect回调,receive回调竟然没有触发成功。经过不断调试后来在官网看到如下一段话得到了启发。

 

  官网说增加监听端口需要单独调用set函数来为addListener返回的swoole_server_port对象设置监听配置注意,只对1.8.0以上版本有效

 

   后来在开启多端口监听中加入了$port->set([]);这一行后发现监听服务可以顺利监听到并且成功触发了recrive

public function run(){

        $setting = Config::get('http');
        /*开启监听*/
        $this->server = new Server($setting['host'],$setting['port']);

        if (isset($setting['config']) && $setting['config'])$this->server->set($setting['config']);


        if (isset($setting['tcp_check']) && $setting['tcp_check']){
            /*开启多端口监听*/
            $port = $this->server->addListener('127.0.0.1','9501',SWOOLE_SOCK_TCP);
            $port->set([]);
            $port->on('connect',[$this,'connect']);
            $port->on('receive',[$this,'receive']);
        }

        $this->server->on('start',[$this,'start']);
        $this->server->on('request',[$this,'request']);

        $this->server->on('workerStart',[$this,'workerStart']);

        $this->server->start();
    }

  收到结果如下:

[root@bin]# php walter.php 
string(10) "connection"
string(7) "receive"
string(11) "hello world"

  虽然不知道这个是不是swoole目前的bug,官网上暂时没看到类似的问题跟文档说明。但是这个是实实在在需要注意的。每一个server都必须调用set函数设置需要的配置,哪怕你什么都不想要设置都要给个空数组进去以防万一。

  另外还有一个需要注意的当监听多端口的时候swoole_server_port->set()设置worker_num数量的时候这里的worker进程不会另外创建,应该是从主进程创建的worker中创建的worker中分配相应数量的进程给swoole_server_port的。所以这里设置的数量比主进程设置的多是没有用的。主进程设置生成的worker数量是多少就多少。以上只是个人猜测,至于swoole底层如何处理这个swoole_server_port设置的worker_num配置不得而知,还没真正看过底层的实现代码。也有人的说法是swoole_server_port设置worker_num是不生效的。哪种说法正确只有以后有机会看看底层源码来判断了。或许这个也是当前版本的bug也说不定。

  当前测试的swoole版本为4.2.8

 

posted @ 2019-03-28 16:08  #walter#  阅读(342)  评论(0编辑  收藏  举报