Skip to content

Commit

Permalink
Merge pull request #18 from GBSL-Informatik/feature/emit-room-updates
Browse files Browse the repository at this point in the history
Feature/emit room updates
  • Loading branch information
lebalz authored Sep 5, 2024
2 parents d9306c2 + 7494048 commit d74a3bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/routes/socketEventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export interface ChangedDocument {
}

export interface ConnectedClients {
room: string;
count: number;
rooms: [string, number][];
type: 'full' | 'update';
}

export interface DeletedRecord {
Expand Down
14 changes: 12 additions & 2 deletions src/routes/socketEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const EventRouter = (io: Server<ClientToServerEvents, ServerToClientEvents>) =>
socket.leave(roomId);
callback();
});
const rooms = [...io.sockets.adapter.rooms.keys()].map(
(id) => [id, io.sockets.adapter.rooms.get(id)?.size || 0] as [string, number]
);
io.to(IoRoom.ADMIN).emit(IoEvent.CONNECTED_CLIENTS, { rooms: rooms, type: 'full' });
}
socket.join(IoRoom.ALL);
const groups = await StudentGroup.all(user);
Expand All @@ -54,11 +58,17 @@ const EventRouter = (io: Server<ClientToServerEvents, ServerToClientEvents>) =>

io.of('/').adapter.on('join-room', (room, id) => {
const size = io.sockets.adapter.rooms.get(room)?.size || 0;
io.to(room).emit(IoEvent.CONNECTED_CLIENTS, { room, count: size });
io.to([room, IoRoom.ADMIN]).emit(IoEvent.CONNECTED_CLIENTS, {
rooms: [[room, size]],
type: 'update'
});
});
io.of('/').adapter.on('leave-room', (room, id) => {
const size = io.sockets.adapter.rooms.get(room)?.size || 0;
io.to(room).except(id).emit(IoEvent.CONNECTED_CLIENTS, { room, count: size });
io.to([room, IoRoom.ADMIN]).emit(IoEvent.CONNECTED_CLIENTS, {
rooms: [[room, size]],
type: 'update'
});
});
};

Expand Down

0 comments on commit d74a3bf

Please sign in to comment.