Skip to content

Commit 523d47c

Browse files
committed
Prevent duplicate socket listeners on client side
1 parent f8cd07f commit 523d47c

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

frontend/src/handlers/matchHandler.ts

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export class MatchHandler {
4444
};
4545

4646
private openConnection = () => {
47-
matchSocket.removeAllListeners();
4847
this.initSocketListeners();
4948
matchSocket.connect();
5049
};
@@ -55,43 +54,59 @@ export class MatchHandler {
5554
};
5655

5756
private initSocketListeners = () => {
58-
matchSocket.on(MATCH_FOUND, ({ matchId, user1, user2 }) => {
59-
this.setMatchDetails(matchId, user1, user2);
60-
matchSocket.emit(MATCH_RECEIVED, this.matchId);
61-
});
62-
63-
matchSocket.on(MATCH_IN_PROGRESS, () => {
64-
console.log("Matching in progress... / Match already found!");
65-
});
66-
67-
matchSocket.on(MATCH_SUCCESSFUL, () => {
68-
console.log("Match successful");
69-
this.closeConnection();
70-
});
71-
72-
matchSocket.on(MATCH_UNSUCCESSFUL, () => {
73-
console.log("Match unsuccessful");
74-
this.closeConnection();
75-
});
76-
77-
matchSocket.on(MATCH_TIMEOUT, () => {
78-
console.log("Match timeout");
79-
this.closeConnection();
80-
});
81-
82-
matchSocket.on(SOCKET_DISCONNECT, (reason) => {
83-
if (reason !== SOCKET_CLIENT_DISCONNECT) {
84-
console.log("Oops, something went wrong! Reconnecting...");
85-
}
86-
});
87-
88-
matchSocket.io.on(SOCKET_RECONNECT_SUCCESS, () => {
89-
console.log("Reconnected!");
90-
});
91-
92-
matchSocket.io.on(SOCKET_RECONNECT_FAILED, () => {
93-
console.log("Oops, something went wrong! Please try again later.");
94-
});
57+
if (!matchSocket.hasListeners(MATCH_FOUND)) {
58+
matchSocket.on(MATCH_FOUND, ({ matchId, user1, user2 }) => {
59+
this.setMatchDetails(matchId, user1, user2);
60+
matchSocket.emit(MATCH_RECEIVED, this.matchId);
61+
});
62+
}
63+
64+
if (!matchSocket.hasListeners(MATCH_IN_PROGRESS)) {
65+
matchSocket.on(MATCH_IN_PROGRESS, () => {
66+
console.log("Matching in progress... / Match already found!");
67+
});
68+
}
69+
70+
if (!matchSocket.hasListeners(MATCH_SUCCESSFUL)) {
71+
matchSocket.on(MATCH_SUCCESSFUL, () => {
72+
console.log("Match successful");
73+
this.closeConnection();
74+
});
75+
}
76+
77+
if (!matchSocket.hasListeners(MATCH_UNSUCCESSFUL)) {
78+
matchSocket.on(MATCH_UNSUCCESSFUL, () => {
79+
console.log("Match unsuccessful");
80+
this.closeConnection();
81+
});
82+
}
83+
84+
if (!matchSocket.hasListeners(MATCH_TIMEOUT)) {
85+
matchSocket.on(MATCH_TIMEOUT, () => {
86+
console.log("Match timeout");
87+
this.closeConnection();
88+
});
89+
}
90+
91+
if (!matchSocket.hasListeners(SOCKET_DISCONNECT)) {
92+
matchSocket.on(SOCKET_DISCONNECT, (reason) => {
93+
if (reason !== SOCKET_CLIENT_DISCONNECT) {
94+
console.log("Oops, something went wrong! Reconnecting...");
95+
}
96+
});
97+
}
98+
99+
if (!matchSocket.io.hasListeners(SOCKET_RECONNECT_SUCCESS)) {
100+
matchSocket.io.on(SOCKET_RECONNECT_SUCCESS, () => {
101+
console.log("Reconnected!");
102+
});
103+
}
104+
105+
if (!matchSocket.io.hasListeners(SOCKET_RECONNECT_FAILED)) {
106+
matchSocket.io.on(SOCKET_RECONNECT_FAILED, () => {
107+
console.log("Oops, something went wrong! Please try again later.");
108+
});
109+
}
95110
};
96111

97112
findMatch = (

0 commit comments

Comments
 (0)