优化代码
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
namespace App\Services\WebSocket;
|
||||
|
||||
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\WebSocket\Frame;
|
||||
@@ -76,12 +75,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
// 验证 token 中的用户 ID 是否匹配
|
||||
$tokenUserId = $payload['sub'] ?? null;
|
||||
if ($tokenUserId != $userId) {
|
||||
Log::warning('WebSocket 认证失败:用户 ID 不匹配', [
|
||||
'fd' => $request->fd,
|
||||
'token_user_id' => $tokenUserId,
|
||||
'query_user_id' => $userId
|
||||
]);
|
||||
|
||||
$this->safePush($server, $request->fd, json_encode([
|
||||
'type' => 'error',
|
||||
'data' => [
|
||||
@@ -95,13 +88,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
|
||||
// 验证 token 是否过期
|
||||
if (isset($payload['exp']) && $payload['exp'] < time()) {
|
||||
Log::warning('WebSocket 认证失败:token 已过期', [
|
||||
'fd' => $request->fd,
|
||||
'user_id' => $userId,
|
||||
'exp' => $payload['exp'],
|
||||
'current_time' => time()
|
||||
]);
|
||||
|
||||
$this->safePush($server, $request->fd, json_encode([
|
||||
'type' => 'error',
|
||||
'data' => [
|
||||
@@ -112,18 +98,7 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
$server->disconnect($request->fd);
|
||||
return;
|
||||
}
|
||||
|
||||
Log::info('WebSocket 认证成功', [
|
||||
'fd' => $request->fd,
|
||||
'user_id' => $userId
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::warning('WebSocket 认证失败:无效的 token', [
|
||||
'fd' => $request->fd,
|
||||
'user_id' => $userId,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
$this->safePush($server, $request->fd, json_encode([
|
||||
'type' => 'error',
|
||||
'data' => [
|
||||
@@ -157,19 +132,7 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
'timestamp' => time()
|
||||
]
|
||||
]));
|
||||
|
||||
Log::info('WebSocket 连接已打开', [
|
||||
'fd' => $request->fd,
|
||||
'user_id' => $userId,
|
||||
'ip' => $request->server['remote_addr']
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('WebSocket onOpen 错误', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'fd' => $request->fd
|
||||
]);
|
||||
|
||||
$this->safePush($server, $request->fd, json_encode([
|
||||
'type' => 'error',
|
||||
'data' => [
|
||||
@@ -197,10 +160,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
}
|
||||
return false;
|
||||
} catch (\Exception $e) {
|
||||
Log::warning('WebSocket push 失败', [
|
||||
'fd' => $fd,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -244,12 +203,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
$type = $message['type'];
|
||||
$data = $message['data'] ?? [];
|
||||
|
||||
Log::info('收到 WebSocket 消息', [
|
||||
'fd' => $frame->fd,
|
||||
'user_id' => $userId,
|
||||
'type' => $type
|
||||
]);
|
||||
|
||||
// 处理不同类型的消息
|
||||
switch ($type) {
|
||||
case 'ping':
|
||||
@@ -302,11 +255,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('WebSocket onMessage 错误', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'fd' => $frame->fd
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,22 +283,11 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
|
||||
// 删除该用户的所有频道订阅
|
||||
$this->removeUserFromAllChannels($wsTable, $userId, $fd);
|
||||
|
||||
Log::info('WebSocket 连接已关闭', [
|
||||
'fd' => $fd,
|
||||
'user_id' => $userId,
|
||||
'reactor_id' => $reactorId
|
||||
]);
|
||||
}
|
||||
|
||||
// 删除 fd 映射
|
||||
$wsTable->del('fd:' . $fd);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('WebSocket onClose 错误', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'fd' => $fd
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,12 +419,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
'timestamp' => time()
|
||||
]
|
||||
]));
|
||||
|
||||
Log::info('用户订阅频道', [
|
||||
'user_id' => $userId,
|
||||
'channel' => $channel,
|
||||
'fd' => $frame->fd
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -527,12 +458,6 @@ class WebSocketHandler implements WebSocketHandlerInterface
|
||||
'timestamp' => time()
|
||||
]
|
||||
]));
|
||||
|
||||
Log::info('用户取消订阅频道', [
|
||||
'user_id' => $userId,
|
||||
'channel' => $channel,
|
||||
'fd' => $frame->fd
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Services\WebSocket;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
/**
|
||||
@@ -67,19 +66,8 @@ class WebSocketService
|
||||
// 发送消息
|
||||
$result = $server->push($fd, json_encode($data));
|
||||
|
||||
Log::info('消息已发送给用户', [
|
||||
'user_id' => $userId,
|
||||
'fd' => $fd,
|
||||
'success' => $result
|
||||
]);
|
||||
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('发送消息给用户失败', [
|
||||
'user_id' => $userId,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -125,7 +113,6 @@ class WebSocketService
|
||||
if (strpos($key, 'uid:') !== 0) {
|
||||
continue;
|
||||
}
|
||||
Log::info($key . json_encode($row));
|
||||
$userId = (int)substr($key, 4); // 移除 'uid:' 前缀
|
||||
$fd = (int)$row['value'];
|
||||
|
||||
@@ -146,18 +133,8 @@ class WebSocketService
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('广播消息已发送', [
|
||||
'exclude_user_id' => $excludeUserId,
|
||||
'sent_to' => $count
|
||||
]);
|
||||
|
||||
return $count;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('广播消息失败', [
|
||||
'exclude_user_id' => $excludeUserId,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -198,18 +175,8 @@ class WebSocketService
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('消息已发送到频道', [
|
||||
'channel' => $channel,
|
||||
'sent_to' => $count
|
||||
]);
|
||||
|
||||
return $count;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('发送消息到频道失败', [
|
||||
'channel' => $channel,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -233,9 +200,6 @@ class WebSocketService
|
||||
|
||||
return $count;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取在线用户数量失败', [
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -261,10 +225,6 @@ class WebSocketService
|
||||
|
||||
return $server->isEstablished($fd);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('检查用户在线状态失败', [
|
||||
'user_id' => $userId,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -289,9 +249,6 @@ class WebSocketService
|
||||
|
||||
return $userIds;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取在线用户 ID 列表失败', [
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -324,17 +281,8 @@ class WebSocketService
|
||||
$wsTable->del('uid:' . $userId);
|
||||
$wsTable->del('fd:' . $fd);
|
||||
|
||||
Log::info('用户已断开连接', [
|
||||
'user_id' => $userId,
|
||||
'fd' => $fd
|
||||
]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('断开用户连接失败', [
|
||||
'user_id' => $userId,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user