开启workerman后,能否用不同的子worker,来监听不同的请求?

bobshipwood

问题描述

 $worker = new Worker();
        $worker->count = 4;
        $worker->onWorkerStart = function ($worker) {
            try {
                switch ($worker->id) {
                    case 0:
                       $worker->protocol = 'Workerman\\Protocols\\Websocket';
                        break;
                    case 1:
                       $worker->protocol = 'Workerman\\Protocols\Tcp';
                        break;
                    case 2:
                        $worker->protocol = 'Workerman\\Protocols\\Http';

另外,就是我看了$worker->protocol到底有哪些选择可以用的?

153 1 0
1个回答

walkor 打赏

改协议设置 $worker->protocol 就行, 支持的协议都在 Workerman/Protocols 目录

  • bobshipwood 11天前

    我的Workerman/Protocols 只有Frame,Http。Text,Websocker,Ws,请问是否支持如tco这种协议呢?

  • walkor 11天前

    tcp直接设置 $worker->protocol = ''; ,默认是tcp

  • bobshipwood 11天前

    嗯,谢谢。那再次请问下,可以为不同的worker->id,设置不同的监听端口吗?比如tcp监听6800端口,http监听8080端口?

  • walkor 11天前

    不用那么麻烦,workerman支持直接启动多个进程监听不同端口,代码类似。不需要设置onWorkerStart

    $worker = new Worker('http://0.0.0.0:1234');
    $worker->onMessage = function(){};
    $worker = new Worker('websocket://0.0.0.0:1235');
    $worker->onMessage = function(){};
    Worker::runAll();

  • bobshipwood 11天前

    好的,最后一个问题,workerman中通过onmessage接受的tcp数据,通过$connection再发回去,请问这个connecttion的是发往这个192.168.1.11这个ip和这个ip所对应的端口的吧?

    $worker->onMessage = function($connection, $data) {
    if ($connection->getRemoteIp() == '192.168.1.11' && strstr($data,'beat')) {

                $connection->send($sentstr);
            }

    }

  • walkor 11天前

×
🔝