作为wss客户端,经常没数据过来

webman

刚启动或重启数据正常,问题重现短时间十几分钟,长则几天
没有任何错误,连接正常

<?php

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

require_once __DIR__ . "/../workerman/Autoloader.php";

function timestamp()
{
    list($timestamp, $second) = explode(" ", microtime());
    return (float) sprintf("%.0f", (floatval($timestamp) + floatval($second)) * 1000);
}

$worker = new Worker();
$worker->onWorkerStart = function($worker)
{
    $connection = new AsyncTcpConnection("ws://futures.huobi.fm:443/swap-ws");
    $connection->transport = "ssl";

    $connection->onConnect = function($connection)
    {
        echo sprintf("[onConnect] date=%s\n", date("Y-m-d H:i:s"));
        $connection->send(json_encode(["sub"=>"market.btc-usd.kline.1min", "zip"=>1]));
    };

    $connection->onMessage = function($connection, $data)
    {
        $un_gzip = gzdecode($data);
        if(empty($un_gzip)){
            return;
        }

        $object = json_decode($un_gzip);
        if(json_last_error() > 0){
            return;
        }

        if(!empty($object->ping)){
            echo sprintf("[pong] date=%s\n", date("Y-m-d H:i:s"));
            $connection->send(json_encode(["pong"=>timestamp()]));
        }

        if(!empty($object->ch)){
            echo sprintf("[onMessage] close=%.2f, date=%s\n", $object->tick->close, date("Y-m-d H:i:s"));
        }
    };

    $connection->onError = function($connection, $code, $msg)
    {
        echo sprintf("[onError] code=%s, message=%s, date=%s\n", $code, $msg, date("Y-m-d H:i:s"));
    };

    $connection->onClose = function($connection)
    {
        echo sprintf("[onClose] date=%s\n", date("Y-m-d H:i:s"));
        $connection->reConnect(1);
    };

    $connection->connect();
};

Worker::runAll();

2057 1 0
1个回答

six

tcpdump -Ans 4000 -iany port 37914

抓包看是不是有数据,有可能真的没有数据发过来。37914是你截图里的本地端口。

  • webman 2020-08-24

    还真没数据,有pong帧总不至于服务端主动断开,我尝试一下ping值作为pong值使用试试

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