-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoom.ts
40 lines (33 loc) · 844 Bytes
/
Room.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Server, Socket } from "socket.io";
export class Room {
id: string; // Rooms id
state: any; // Rooms state
io: Server;
constructor(io: Server, id: string) {
this.io = io;
this.id = id;
this.state = {roomId: id};
}
updateState(dState: any) {
// Change this.state
this.state = { ...this.state, ...dState };
// Notify room
this.emit("updateState", this.state);
}
emit(event: string, payload: any) {
this.io.to(this.id).emit(`${this.id}:${event}`, payload);
}
enter(socket: Socket) {
socket.join(this.id);
this.log(`${socket.id} has entered`);
}
leave(socket: Socket) {
socket.leave(this.id);
this.log(`${socket.id} has left`);
}
log(message) {
const output = `[${this.id}] ${message}`;
console.log(output);
this.io.emit("debug", output);
}
}