Skip to content

Commit 72423ac

Browse files
authored
Merge pull request #354 from aadeshkulkarni/chore/socket-singleton
Chore | Singleton implementation for Sockets
2 parents 4d018e1 + 67ed606 commit 72423ac

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

apps/ws/src/Game.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from './messages';
77
import { db } from './db';
88
import { randomUUID } from 'crypto';
9-
import { SocketManager, User } from './SocketManager';
9+
import { socketManager, User } from './SocketManager';
1010

1111
type GAME_STATUS = 'IN_PROGRESS' | 'COMPLETED' | 'ABANDONED' | 'TIME_UP';
1212
type GAME_RESULT = "WHITE_WINS" | "BLACK_WINS" | "DRAW";
@@ -125,7 +125,7 @@ export class Game {
125125
return;
126126
}
127127

128-
SocketManager.getInstance().broadcast(
128+
socketManager.broadcast(
129129
this.gameId,
130130
JSON.stringify({
131131
type: INIT_GAME,
@@ -257,7 +257,7 @@ export class Game {
257257

258258
this.lastMoveTime = moveTimestamp;
259259

260-
SocketManager.getInstance().broadcast(
260+
socketManager.broadcast(
261261
this.gameId,
262262
JSON.stringify({
263263
type: MOVE,
@@ -333,7 +333,7 @@ export class Game {
333333
}
334334
});
335335

336-
SocketManager.getInstance().broadcast(
336+
socketManager.broadcast(
337337
this.gameId,
338338
JSON.stringify({
339339
type: GAME_ENDED,

apps/ws/src/GameManager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from './messages';
1515
import { Game, isPromoting } from './Game';
1616
import { db } from './db';
17-
import { SocketManager, User } from './SocketManager';
17+
import { socketManager, User } from './SocketManager';
1818
import { Square } from 'chess.js';
1919
import { GameStatus } from '@prisma/client';
2020

@@ -41,7 +41,7 @@ export class GameManager {
4141
return;
4242
}
4343
this.users = this.users.filter((user) => user.socket !== socket);
44-
SocketManager.getInstance().removeUser(user);
44+
socketManager.removeUser(user);
4545
}
4646

4747
removeGame(gameId: string) {
@@ -59,7 +59,7 @@ export class GameManager {
5959
return;
6060
}
6161
if (user.userId === game.player1UserId) {
62-
SocketManager.getInstance().broadcast(
62+
socketManager.broadcast(
6363
game.gameId,
6464
JSON.stringify({
6565
type: GAME_ALERT,
@@ -70,15 +70,15 @@ export class GameManager {
7070
);
7171
return;
7272
}
73-
SocketManager.getInstance().addUser(user, game.gameId);
73+
socketManager.addUser(user, game.gameId);
7474
await game?.updateSecondPlayer(user.userId);
7575
this.pendingGameId = null;
7676
} else {
7777
const game = new Game(user.userId, null);
7878
this.games.push(game);
7979
this.pendingGameId = game.gameId;
80-
SocketManager.getInstance().addUser(user, game.gameId);
81-
SocketManager.getInstance().broadcast(
80+
socketManager.addUser(user, game.gameId);
81+
socketManager.broadcast(
8282
game.gameId,
8383
JSON.stringify({
8484
type: GAME_ADDED,
@@ -182,7 +182,7 @@ export class GameManager {
182182
}),
183183
);
184184

185-
SocketManager.getInstance().addUser(user, gameId);
185+
socketManager.addUser(user, gameId);
186186
}
187187
});
188188
}

apps/ws/src/SocketManager.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class User {
1313
}
1414
}
1515

16-
export class SocketManager {
16+
class SocketManager {
1717
private static instance: SocketManager;
1818
private interestedSockets: Map<string, User[]>;
1919
private userRoomMappping: Map<string, string>;
@@ -24,12 +24,12 @@ export class SocketManager {
2424
}
2525

2626
static getInstance() {
27-
if (this.instance) {
28-
return this.instance;
27+
if (SocketManager.instance) {
28+
return SocketManager.instance;
2929
}
3030

31-
this.instance = new SocketManager();
32-
return this.instance;
31+
SocketManager.instance = new SocketManager();
32+
return SocketManager.instance;
3333
}
3434

3535
addUser(user: User, roomId: string) {
@@ -72,3 +72,5 @@ export class SocketManager {
7272
this.userRoomMappping.delete(user.userId);
7373
}
7474
}
75+
76+
export const socketManager = SocketManager.getInstance()

0 commit comments

Comments
 (0)