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