I WebsocketService

This commit is contained in:
a6a2f5842 2025-09-15 12:40:25 +02:00
parent fbb789b25e
commit ba53960551
1 changed files with 24 additions and 10 deletions

View File

@ -37,7 +37,7 @@ class WebsocketService
public static function getAuth(string $socketId) public static function getAuth(string $socketId)
{ {
config(['cache.default' => 'file']); config(['cache.default' => 'file']);
return cache()->get('ws_socket_auth_' . $socketId); return cache()->get('ws_socket_auth_' . str()->slug($socketId));
} }
public static function getChannelConnections(string $channelName) public static function getChannelConnections(string $channelName)
@ -55,7 +55,7 @@ class WebsocketService
public static function getConnection(string $socketId) public static function getConnection(string $socketId)
{ {
config(['cache.default' => 'file']); config(['cache.default' => 'file']);
return cache()->get('ws_connection_' . $socketId); return cache()->get('ws_connection_' . str()->slug($socketId));
} }
public static function getAuthedUsers() public static function getAuthedUsers()
@ -66,20 +66,14 @@ class WebsocketService
public static function isUserConnected($userId) public static function isUserConnected($userId)
{ {
config(['cache.default' => 'file']); return in_array($userId, array_values(static::getAuthedUsers()));
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
$user_ids = array_values($authed_users);
return in_array($userId, $user_ids);
} }
public static function getUserSocketIds($userId) public static function getUserSocketIds($userId)
{ {
config(['cache.default' => 'file']);
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
$socket_ids = []; $socket_ids = [];
foreach ($authed_users as $socket_id => $u_id) { foreach (static::getAuthedUsers() as $socket_id => $u_id) {
if ($u_id == $userId) { if ($u_id == $userId) {
$socket_ids[] = $socket_id; $socket_ids[] = $socket_id;
} }
@ -87,4 +81,24 @@ class WebsocketService
return $socket_ids; return $socket_ids;
} }
public static function setUserAuthed($socketId, $user)
{
$authed_users = static::getAuthedUsers();
$authed_users[$socketId] = $user->id;
cache()->forever('ws_socket_authed_users', $authed_users);
cache()->forever('ws_socket_auth_' . str()->slug($socketId), $user);
return static::getAuthedUsers();
}
public static function clearUserAuthed($socketId)
{
$authed_users = static::getAuthedUsers();
unset($authed_users[$socketId]);
cache()->forever('ws_socket_authed_users', $authed_users);
cache()->forget('ws_socket_auth_' . str()->slug($socketId));
return static::getAuthedUsers();
}
} }