优化代码

This commit is contained in:
2026-02-18 22:36:40 +08:00
parent 0ecb088569
commit 736a41a718
2 changed files with 0 additions and 127 deletions

View File

@@ -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;
}
}