feat(signalr): kick device on a login on another device

This commit is contained in:
MingxuanGame
2025-08-11 13:23:04 +00:00
parent 1c233ed31c
commit bc5ae3083a
2 changed files with 20 additions and 0 deletions

View File

@@ -99,6 +99,16 @@ class Hub[TState: UserState]:
return client
return default
def get_before_clients(self, id: str, current_token: str) -> list[Client]:
clients = []
for client in self.clients.values():
if client.connection_id != id:
continue
if client.connection_token == current_token:
continue
clients.append(client)
return clients
@abstractmethod
def create_state(self, client: Client) -> TState:
raise NotImplementedError
@@ -117,6 +127,11 @@ class Hub[TState: UserState]:
if group_id in self.groups:
self.groups[group_id].discard(client)
async def kick_client(self, client: Client) -> None:
await self.call_noblock(client, "DisconnectRequested")
await client.send_packet(ClosePacket(allow_reconnect=False))
await client.connection.close(code=1000, reason="Disconnected by server")
async def add_client(
self,
connection_id: str,

View File

@@ -98,6 +98,11 @@ async def connect(
if error or not client:
await websocket.close(code=1008)
return
connected_clients = hub_.get_before_clients(user_id, id)
for connected_client in connected_clients:
await hub_.kick_client(connected_client)
await hub_.clean_state(client, False)
task = asyncio.create_task(hub_.on_connect(client))
hub_.tasks.add(task)