@@ -44,7 +44,6 @@ export class MatchHandler {
44
44
} ;
45
45
46
46
private openConnection = ( ) => {
47
- matchSocket . removeAllListeners ( ) ;
48
47
this . initSocketListeners ( ) ;
49
48
matchSocket . connect ( ) ;
50
49
} ;
@@ -55,43 +54,59 @@ export class MatchHandler {
55
54
} ;
56
55
57
56
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
+ }
95
110
} ;
96
111
97
112
findMatch = (
0 commit comments