× 警告!旧版文档已经暂停维护,请查看新版文档。点击前往新版文档

在worker进程内监听一个Server端口

注意, 此文档内容可能已过期, swoole最新版支持多协议监听

在一些场景下,需要监听额外的端口提供特殊协议处理。如在HttpServer中需要监听8081端口,提供管理Server的功能。在Swoole扩展内置的服务中不支持同时处理2种协议,即使是使用了addlistener添加了多个端口也不能接受2种协议的请求包。

这时候可以使用本地监听来解决此问题,原理是在某一个worker进程内,创建stream_socket_server,并加入到swoole_event中。

$serv = new swoole_server("0.0.0.0", 9502);

$serv->on('workerstart', function($server, $id) {
    //仅在worker-0中监听管理端口
    if ($id != 0) return;
    $local_listener = stream_socket_server("tcp://127.0.0.1:8081", $errno, $errstr);
    swoole_event_add($local_listener, function($server) {
          $local_client = stream_socket_accept($server, 0);       
          swoole_event_add($local_client, function($client) {
              echo fread($client, 8192);
              fwrite($client, "hello");
          });
     });
});

  • 云剑仙Cloud

    多看

  • dawn

    在Swoole扩展内置的服务中不支持同时处理2种协议??不是说支持多端口混合协议吗,我在swoole_websocket_server下监听tcp端口,接收数据也是正常的啊,ws来自onMessage,tcp来自onReceive。

  • 新用户(手机注册)

    我应用此方法,在worker进程内监听一个Server端口,但触发后,回调函数回一直执行,进入死循环。还不能用exit,die。大神们你们是怎么解决的?