mirror of
https://gitee.com/mix-php/mix.git
synced 2024-12-02 03:37:56 +08:00
feat:websocket:优化连接异常错误信息
This commit is contained in:
parent
5cfb4e664e
commit
4f1767dbae
@ -5,6 +5,7 @@ namespace App\Service;
|
||||
use App\Container\Logger;
|
||||
use App\Handler\Hello;
|
||||
use Mix\WebSocket\Connection;
|
||||
use Mix\WebSocket\Exception\CloseFrameException;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
class Session
|
||||
@ -46,11 +47,12 @@ class Session
|
||||
try {
|
||||
$frame = $this->conn->readMessage();
|
||||
} catch (\Throwable $ex) {
|
||||
// 忽略一些异常日志
|
||||
if (!in_array($ex->getMessage(), ['Active closure of the user', 'Connection reset by peer'])) {
|
||||
Logger::instance()->error(sprintf('ReadMessage: %s', $ex->getMessage()));
|
||||
}
|
||||
$this->stop();
|
||||
// 忽略一些异常日志
|
||||
if ($ex instanceof CloseFrameException) {
|
||||
return;
|
||||
}
|
||||
Logger::instance()->error(sprintf('ReadMessage: %s: %s', get_class($ex), $ex->getMessage()));
|
||||
return;
|
||||
}
|
||||
$message = $frame->data;
|
||||
|
@ -54,9 +54,11 @@ class Connection
|
||||
$this->receiving = false;
|
||||
if (!$frame) { // 接收失败
|
||||
$this->close(); // 需要移除管理器内的连接,所以还要 close
|
||||
// 连接关闭返回空字符串
|
||||
if ($frame === '') {
|
||||
throw new ReadMessageException('Connection is closed');
|
||||
}
|
||||
// 失败返回 false,请使用 swoole_last_error() 获取错误码
|
||||
$errCode = swoole_last_error();
|
||||
if ($errCode != 0) {
|
||||
$errMsg = swoole_strerror($errCode, 9);
|
||||
@ -66,7 +68,8 @@ class Connection
|
||||
if ($frame instanceof \Swoole\WebSocket\CloseFrame) { // CloseFrame
|
||||
$this->close(); // 需要移除管理器内的连接,所以还要 close
|
||||
$errCode = $frame->code;
|
||||
$errMsg = $frame->reason;
|
||||
// Active closure of the user
|
||||
$errMsg = $frame->reason ?: 'Reason is empty';
|
||||
throw new CloseFrameException($errMsg, $errCode);
|
||||
}
|
||||
return $frame;
|
||||
|
@ -25,12 +25,12 @@ class Upgrader
|
||||
{
|
||||
// Handshake verification
|
||||
if ($request->getHeaderLine('connection') !== 'Upgrade' || $request->getHeaderLine('upgrade') !== 'websocket') {
|
||||
throw new UpgradeException('Handshake failed, invalid WebSocket request');
|
||||
throw new UpgradeException('Handshake failed, invalid websocket request');
|
||||
}
|
||||
$secWebSocketKey = $request->getHeaderLine('sec-websocket-key');
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if ($request->getHeaderLine('sec-websocket-version') != 13 || 0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
throw new UpgradeException('Handshake failed, invalid WebSocket protocol v13');
|
||||
throw new UpgradeException('Handshake failed, invalid websocket protocol v13');
|
||||
}
|
||||
|
||||
// Upgrade
|
||||
@ -57,12 +57,12 @@ class Upgrader
|
||||
{
|
||||
// Handshake verification
|
||||
if (($request->header['connection'] ?? '') !== 'Upgrade' || ($request->header['upgrade'] ?? '') !== 'websocket') {
|
||||
throw new UpgradeException('Handshake failed, invalid WebSocket request');
|
||||
throw new UpgradeException('Handshake failed, invalid websocket request');
|
||||
}
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'] ?? '';
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (($request->header['sec-websocket-version'] ?? '') != 13 || 0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
throw new UpgradeException('Handshake failed, invalid WebSocket protocol v13');
|
||||
throw new UpgradeException('Handshake failed, invalid websocket protocol v13');
|
||||
}
|
||||
|
||||
// Upgrade
|
||||
|
Loading…
Reference in New Issue
Block a user