channel client 启动的时候一连接就报错

jie365@126.com

PHP Warning: stream_socket_client(): unable to connect to tcp://:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /data/www/admin/PushCommandService/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 195

    public function connect()
    {
        if ($this->_status !== self::STATUS_INITIAL && $this->_status !== self::STATUS_CLOSING &&
            $this->_status !== self::STATUS_CLOSED) {
            return;
        }
        $this->_status           = self::STATUS_CONNECTING;
        $this->_connectStartTime = \microtime(true);
        if ($this->transport !== 'unix') {
            if (!$this->_remotePort) {
                $this->_remotePort = $this->transport === 'ssl' ? 443 : 80;
                $this->_remoteAddress = $this->_remoteHost.':'.$this->_remotePort;
            }
            // Open socket connection asynchronously.
            if ($this->_contextOption) {
                $context = \stream_context_create($this->_contextOption);
                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
                    $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT, $context);
            } else {
                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
                    $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT);
            }
        } else {
            $this->_socket = \stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
                \STREAM_CLIENT_ASYNC_CONNECT);
        }
        // If failed attempt to emit onError callback.
        if (!$this->_socket || !\is_resource($this->_socket)) {
            $this->emitError(\WORKERMAN_CONNECT_FAIL, $errstr);
            if ($this->_status === self::STATUS_CLOSING) {
                $this->destroy();
            }
            if ($this->_status === self::STATUS_CLOSED) {
                $this->onConnect = null;
            }
            return;
        }
        // Add socket to global event loop waiting connection is successfully established or faild.
        Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
        // For windows.
        if(\DIRECTORY_SEPARATOR === '\\') {
            Worker::$globalEvent->add($this->_socket, EventInterface::EV_EXCEPT, array($this, 'checkConnection'));
        }
    }

我的channel server 监听的是172.16.0.177:2206
channel client 连接的也是 172.16.0.177:2206
而且我没有使用80端口

$this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
$errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT);

这行为何启动的时候warnning?

1554 1 1
1个回答

six

unable to connect to tcp://:80,很明显,域名/ip没有值

  • jie365@126.com 2022-01-17

    关键是我没有监听80端口

  • six 2022-01-17

    channel/client 代码呢

  • jie365@126.com 2022-01-17

    //定时ping(心跳)
    Channel\Client::$pingInterval = 10;

        //连接到channel服务器
        Channel\Client::connect($this->configInfo['channel_server']['host'], $this->configInfo['channel_server']['port']);
    
        Channel\Client::$onConnect = function () {
            echo "{$this->nodeName}已经连接上channel server" . PHP_EOL;
        };
    
        Channel\Client::$onMessage = function ($eventName, $eventData) {
            echo "{$this->nodeName}接收到channel server事件:{$eventName},内容:{$eventData}" . PHP_EOL;
        };
    
        Channel\Client::$onClose = function () {
            echo "{$this->nodeName}已经与channel server失去连接" . PHP_EOL;
        }
  • jie365@126.com 2022-01-17

    读的都是配置文件,channel server 启动的时候都是用的这个配置文件

  • six 2022-01-17

    打印下配置看下对不对呗

  • jie365@126.com 2022-01-17

    AsyncTcpConnection的构造器里加的打印信息
    public function __construct($remote_address, array $context_option = array())
    {
    error_log("--------{$remote_address}--------");

    ======分隔线=====
    打印信息如下
    --------frame://172.16.0.177:2206--------

    是完全跟我的配置对应的

  • jie365@126.com 2022-01-17

    构造器最后拿到的信息
    error_log("----{$this->transport}----{$this->_remoteHost}----{$this->_remotePort}----{$this->_remoteAddress}");

    ----tcp----172.16.0.177----2206----172.16.0.177:2206

  • six 2022-01-17

    $this->_remotePort = $this->transport === 'ssl' ? 443 : 80;

    从你贴的代码看,如果没传端口号的话,自动用80或443,所以我感觉是你的配置有时候可能读不到,导致ip为空,端口默认用了80?

  • jie365@126.com 2022-01-17

    有可能

年代过于久远,无法发表回答
×
🔝