2025-05-08 08:54:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Services;
|
|
|
|
|
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Events\WebsocketMessageEvent;
|
|
|
|
|
|
|
|
|
|
class WebsocketService
|
|
|
|
|
{
|
|
|
|
|
public static function send($data)
|
|
|
|
|
{
|
|
|
|
|
// TODO make work to send via websocket from anywhere
|
|
|
|
|
// WebsocketMessageEvent::dispatch(
|
|
|
|
|
// optional(optional(tenant())->tenantable)->public_id,
|
|
|
|
|
// $d['event'],
|
|
|
|
|
// (is_array($d['data']))
|
|
|
|
|
// ? $d['data']
|
|
|
|
|
// : ['data' => $d['data']]
|
|
|
|
|
// );
|
|
|
|
|
}
|
2025-09-14 13:00:27 +00:00
|
|
|
|
2025-09-15 08:23:07 +00:00
|
|
|
public static function resetAllTracking()
|
|
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
|
|
|
|
cache()->forget('ws_active_channels');
|
|
|
|
|
cache()->forget('ws_socket_auth');
|
|
|
|
|
cache()->forget('ws_socket_auth_users');
|
2025-09-15 08:25:38 +00:00
|
|
|
cache()->forget('ws_socket_authed_users');
|
2025-09-15 08:23:07 +00:00
|
|
|
cache()->forget('ws_channel_connections');
|
|
|
|
|
cache()->forget('ws_connection');
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 08:25:38 +00:00
|
|
|
|
2025-09-15 08:23:07 +00:00
|
|
|
public static function getAuth(string $socketId)
|
2025-09-14 13:00:27 +00:00
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
2025-09-15 10:40:25 +00:00
|
|
|
return cache()->get('ws_socket_auth_' . str()->slug($socketId));
|
2025-09-14 13:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getChannelConnections(string $channelName)
|
|
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
|
|
|
|
return cache()->get('ws_channel_connections_' . $channelName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getActiveChannels()
|
|
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
|
|
|
|
return cache()->get('ws_active_channels');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getConnection(string $socketId)
|
|
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
2025-09-15 10:40:25 +00:00
|
|
|
return cache()->get('ws_connection_' . str()->slug($socketId));
|
2025-09-14 13:00:27 +00:00
|
|
|
}
|
2025-09-15 08:20:13 +00:00
|
|
|
|
|
|
|
|
public static function getAuthedUsers()
|
|
|
|
|
{
|
|
|
|
|
config(['cache.default' => 'file']);
|
|
|
|
|
return cache()->get('ws_socket_authed_users') ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function isUserConnected($userId)
|
|
|
|
|
{
|
2025-09-15 10:40:25 +00:00
|
|
|
return in_array($userId, array_values(static::getAuthedUsers()));
|
2025-09-15 08:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUserSocketIds($userId)
|
|
|
|
|
{
|
|
|
|
|
$socket_ids = [];
|
|
|
|
|
|
2025-09-15 10:40:25 +00:00
|
|
|
foreach (static::getAuthedUsers() as $socket_id => $u_id) {
|
2025-09-15 08:20:13 +00:00
|
|
|
if ($u_id == $userId) {
|
|
|
|
|
$socket_ids[] = $socket_id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $socket_ids;
|
|
|
|
|
}
|
2025-09-15 10:40:25 +00:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2025-05-08 08:54:11 +00:00
|
|
|
}
|