服務(wù):GatewayWorker
客戶端:AsyncTcpConnection
客戶端 向 服務(wù) 發(fā)送數(shù)據(jù),服務(wù)處理數(shù)據(jù)報錯關(guān)閉鏈接,客戶端沒有觸發(fā)onClose回調(diào),連定時器也沒有執(zhí)行,好像是卡著在某段代碼,沒有任何反應(yīng)。
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;
/**
* 創(chuàng)捷websocket客戶端連接通訊服務(wù)器
* 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 "鏈接關(guān)閉:{$connection->id}\r\n";
};
$this->conn->onError = function (AsyncTcpConnection $connection, $errCode, $errMsg) {
echo "鏈接錯誤:{$connection->id}--{$errMsg}[{$errCode}]\r\n";
};
$this->conn->connect();
}
}
第一步:啟動GatewayWorker服務(wù);
第二步:啟動客戶端,客戶端使用AsyncTcpConnection鏈接服務(wù),成功后向服務(wù)發(fā)送數(shù)據(jù);
第三步:服務(wù)處理接受數(shù)據(jù),數(shù)據(jù)處理異常導(dǎo)致鏈接關(guān)閉
系統(tǒng):Windows 11 專業(yè)版