Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

连接wss协议时,报了一个很奇怪的错Websocket upgrade failed by [Connection timed out] #149

Open
zwl7 opened this issue Mar 8, 2023 · 1 comment

Comments

@zwl7
Copy link

zwl7 commented Mar 8, 2023

具体错误信息是
[2023-03-08 11:29:07 $22941.0] WARNING check_worker_exit_status: worker#0[pid=22991] abnormal exit, status=255, signal=0
PHP Fatal error: Uncaught Swlib\Http\Exception\ConnectException: HTTP 110 Unknown: Websocket upgrade failed by [Connection timed out]. in /home/wwwroot/huobiWebsocketPhp/vendor/swlib/saber/src/WebSocket.php:67
Stack trace:
#0 /home/wwwroot/huobiWebsocketPhp/vendor/swlib/saber/src/Saber.php(314): Swlib\Saber\WebSocket->__construct()
#1 /home/wwwroot/huobiWebsocketPhp/vendor/swlib/saber/src/Saber.php(140): Swlib\Saber->upgrade()
#2 /home/wwwroot/huobiWebsocketPhp/vendor/swlib/saber/src/SaberGM.php(101): Swlib\Saber::websocket()
#3 /home/wwwroot/huobiWebsocketPhp/src/Websocket.php(238): Swlib\SaberGM::websocket()
#4 /home/wwwroot/huobiWebsocketPhp/src/Websocket.php(82): Zwl\HuobiWebsocketPhp\Websocket->connectWs()
#5 {main}
thrown in /home/wwwroot/huobiWebsocketPhp/vendor/swlib/saber/src/WebSocket.php on line 67

我的swoole版本
swoole

Swoole => enabled
Author => Swoole Team team@swoole.com
Version => 4.6.7
Built => Oct 17 2021 18:11:07
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.0.2u 20 Dec 2019
pcre => enabled
zlib => 1.2.7
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

我的复现代码,我启动了一个ws服务端,然后再onworkerstart的事件里新建了一个协程去连接第三方的wss接口拿数据。
public function __construct()
{
$this->WebServer = new \Swoole\WebSocket\Server($this->ip, $this->port);
$this->WebServer->set(array(
'worker_num' => $this->workerNum, // 一般设置为服务器CPU数的1-4倍
'daemonize' => 1, // 是否为守护进程 1,0
'heartbeat_idle_time' => 90, // 客户端心跳时间1
'heartbeat_check_interval' => 45, // 每60秒遍历一次
'log_file' => DIR.'/swoole.log', // 日志文件信息1
'reload_async' => true, // 设置异步重启开关
'max_wait_time' => 5 // 设置 Worker 进程收到停止服务通知后最大等待时间
));

    // 绑定回调事件
    $this->WebServer->on('workerStart', array($this, 'onWorkerStart'));
    $this->WebServer->on('message', array($this, 'onMessage'));
    $this->WebServer->on('workerError', array($this, 'onWorkerError'));
    $this->WebServer->on('workerExit', array($this, 'onWorkerExit'));

    // 开启swoole服务, 程序回阻塞在这里
    $this->WebServer->start();
}

public function onWorkerStart(\Swoole\WebSocket\Server $server, int $workerId)
{

        go(function () {
            $sub       = [
                'sub' => "market.btcusdt.ticker"
            ];

            // 使用SaberGM(官方的链接webscoket客户端包),链接火币的WebSocketUrl(wss://api.huobi.pro/ws)
            $websocket = SaberGM::websocket('wss://api.huobi.pro/ws');

            //订阅对应的行情
            $websocket->push(json_encode($sub));

            var_dump($websocket);
            while (true) {

                // 接收数据
                $recv_data = $websocket->recv(1);

                // 火币返回的数据默认经过了gzip压缩,需要解压
                $data = json_decode(gzdecode($recv_data->getData()), true);
                var_dump($data);
            }
        });

}
@zwl7
Copy link
Author

zwl7 commented Mar 8, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant