AsyncTcpConnection客户端在windows系统下服务器链接断开没有触发回调函数

A文

问题描述

服务:GatewayWorker
客户端:AsyncTcpConnection
客户端 向 服务 发送数据,服务处理数据报错关闭链接,客户端没有触发onClose回调,连定时器也没有执行,好像是卡着在某段代码,没有任何反应。

客户端代码

worker代码

$worker = new Worker('tcp://0.0.0.0:1110');
$worker->count = 1;
$worker->name = 'RedisQueue0';
$worker->onWorkerStart = function (Worker $worker) {
    try {
        $protocol = env('SOCKET_PROTOCOL', 'ws');
        $host = env('SOCKET_HOST', '127.0.0.1');
        $port = env('SOCKET_PORT');
        $worker->websocketClient = new WebsocketClient($protocol . '://' . $host . ($port ? ':' . $port : ''));
    } catch (\Throwable $e) {
       echo "客户端链接异常:{$e->getMessage}  in file {$e->getFile()} on line {$e->getLine()}\r\n";
    }
};

AsyncTcpConnection代码:

use Workerman\Connection\AsyncTcpConnection;
use Workerman\Timer;
class WebsocketClient
{
    /**
     * 连接资源
     * @var AsyncTcpConnection|null
     */
    protected $conn = null;
    /**
     * @var null
     */
    protected $timer = null;
    /**
     * 心跳时间
     * @var null
     */
    protected $heartbeat_time = 30;
    /**
     * 最后一次通讯时间
     * Summary of last_time
     * @var 
     */
    protected $last_time = null;
    /**
     * Summary of remoteAddress
     * @var 
     */
    protected $remoteAddress;
    /**
     * 创捷websocket客户端连接通讯服务器
     * Summary of __construct
     * @param mixed $processName
     * @param mixed $remoteAddress
     */
    public function __construct($remoteAddress = 'ws://127.0.0.1:8282')
    {
        $this->remoteAddress = $remoteAddress;
        $this->conn = new AsyncTcpConnection($remoteAddress);
        $this->conn->onConnect = function (AsyncTcpConnection $connection) {
           echo "链接成功:{$connection->id}\r\n";
           $this->conn->send(json_encode([
                    'type' => 'data',
                    'data' => [
                        'id'=>1
                    ],
                ]));
           $this->timer = Timer::add($this->heartbeat_time, function () {
                $this->conn->send(json_encode([
                    'type' => 'ping',
                    'data' => 'pong',
                ]));
            });
        };
        $this->conn->onMessage = function (AsyncTcpConnection $connection, $message) {
            echo "接受消息:{$message}\r\n";
        };
        $this->conn->onClose = function (AsyncTcpConnection $connection) {
            echo "链接关闭:{$connection->id}\r\n";
        };
        $this->conn->onError = function (AsyncTcpConnection $connection, $errCode, $errMsg) {
            echo "链接错误:{$connection->id}--{$errMsg}[{$errCode}]\r\n";
        };
        $this->conn->connect();
    }
}

重现问题的步骤

第一步:启动GatewayWorker服务;
第二步:启动客户端,客户端使用AsyncTcpConnection链接服务,成功后向服务发送数据;
第三步:服务处理接受数据,数据处理异常导致链接关闭

操作系统环境及workerman/webman等具体版本

系统:Windows 11 专业版
截图

118 0 0
0个回答

×
🔝