1
- import { io , Socket } from "socket.io-client" ;
2
1
import {
3
2
MATCH_ACCEPTED ,
4
3
MATCH_DECLINED ,
5
4
MATCH_FOUND ,
5
+ MATCH_IN_PROGRESS ,
6
6
MATCH_RECEIVED ,
7
7
MATCH_REQUEST ,
8
8
MATCH_SUCCESSFUL ,
@@ -11,88 +11,114 @@ import {
11
11
SOCKET_CLIENT_DISCONNECT ,
12
12
SOCKET_DISCONNECT ,
13
13
SOCKET_RECONNECT_FAILED ,
14
+ SOCKET_RECONNECT_SUCCESS ,
14
15
} from "../utils/constants" ;
15
- import { User } from "../types/types " ;
16
+ import { matchSocket } from "../utils/matchSocket " ;
16
17
17
- const SOCKET_URL = "http://localhost:3002" ;
18
+ interface MatchUser {
19
+ id : string ;
20
+ username : string ;
21
+ profile ?: string ;
22
+ }
18
23
19
24
export class MatchHandler {
20
- socket : Socket ;
21
25
matchId ?: string ;
26
+ user : MatchUser ;
27
+ partner ?: MatchUser ;
22
28
23
- constructor ( ) {
24
- this . socket = io ( SOCKET_URL , {
25
- reconnectionAttempts : 3 ,
26
- } ) ;
29
+ constructor ( user : MatchUser ) {
30
+ this . user = user ;
27
31
}
28
32
29
- findMatch = (
30
- user : User ,
31
- complexities : string [ ] ,
32
- categories : string [ ] ,
33
- languages : string [ ] ,
34
- timeout : number
33
+ private setMatchDetails = (
34
+ matchId : string ,
35
+ user1 : MatchUser ,
36
+ user2 : MatchUser
35
37
) => {
36
- this . socket . emit ( MATCH_REQUEST , {
37
- user : {
38
- id : user . id ,
39
- username : user . username ,
40
- profile : user . profilePictureUrl ,
41
- } ,
42
- complexities : complexities ,
43
- categories : categories ,
44
- languages : languages ,
45
- timeout : timeout ,
38
+ this . matchId = matchId ;
39
+ user1 . id !== this . user . id ? ( this . partner = user1 ) : ( this . partner = user2 ) ;
40
+
41
+ console . log ( `Match ID: ${ this . matchId } ` ) ;
42
+ console . log ( `User: ${ this . user ! . username } ` ) ;
43
+ console . log ( `Partner: ${ this . partner ! . username } ` ) ;
44
+ } ;
45
+
46
+ private openConnection = ( ) => {
47
+ matchSocket . removeAllListeners ( ) ;
48
+ this . initSocketListeners ( ) ;
49
+ matchSocket . connect ( ) ;
50
+ } ;
51
+
52
+ private closeConnection = ( ) => {
53
+ matchSocket . removeAllListeners ( ) ;
54
+ matchSocket . disconnect ( ) ;
55
+ } ;
56
+
57
+ private initSocketListeners = ( ) => {
58
+ matchSocket . on ( MATCH_FOUND , ( { matchId, user1, user2 } ) => {
59
+ this . setMatchDetails ( matchId , user1 , user2 ) ;
60
+ matchSocket . emit ( MATCH_RECEIVED , this . matchId ) ;
46
61
} ) ;
47
62
48
- this . socket . on ( MATCH_FOUND , ( { matchId, user1, user2 } ) => {
49
- console . log ( `Match ID: ${ matchId } ` ) ;
50
- console . log ( `User 1: ${ user1 . username } ` ) ;
51
- console . log ( `User 2: ${ user2 . username } ` ) ;
52
- this . matchId = matchId ;
53
- this . socket . emit ( MATCH_RECEIVED , this . matchId ) ;
63
+ matchSocket . on ( MATCH_IN_PROGRESS , ( ) => {
64
+ console . log ( "Matching in progress... / Match already found!" ) ;
54
65
} ) ;
55
66
56
- this . socket . on ( MATCH_SUCCESSFUL , ( ) => {
67
+ matchSocket . on ( MATCH_SUCCESSFUL , ( ) => {
57
68
console . log ( "Match successful" ) ;
58
69
this . closeConnection ( ) ;
59
70
} ) ;
60
71
61
- this . socket . on ( MATCH_UNSUCCESSFUL , ( ) => {
72
+ matchSocket . on ( MATCH_UNSUCCESSFUL , ( ) => {
62
73
console . log ( "Match unsuccessful" ) ;
63
74
this . closeConnection ( ) ;
64
75
} ) ;
65
76
66
- this . socket . on ( MATCH_TIMEOUT , ( ) => {
77
+ matchSocket . on ( MATCH_TIMEOUT , ( ) => {
67
78
console . log ( "Match timeout" ) ;
68
79
this . closeConnection ( ) ;
69
80
} ) ;
70
81
71
- this . socket . on ( SOCKET_DISCONNECT , ( reason ) => {
82
+ matchSocket . on ( SOCKET_DISCONNECT , ( reason ) => {
72
83
if ( reason !== SOCKET_CLIENT_DISCONNECT ) {
73
84
console . log ( "Oops, something went wrong! Reconnecting..." ) ;
74
85
}
75
86
} ) ;
76
87
77
- this . socket . io . on ( SOCKET_RECONNECT_FAILED , ( ) => {
88
+ matchSocket . io . on ( SOCKET_RECONNECT_SUCCESS , ( ) => {
89
+ console . log ( "Reconnected!" ) ;
90
+ } ) ;
91
+
92
+ matchSocket . io . on ( SOCKET_RECONNECT_FAILED , ( ) => {
78
93
console . log ( "Oops, something went wrong! Please try again later." ) ;
79
94
} ) ;
80
95
} ;
81
96
97
+ findMatch = (
98
+ complexities : string [ ] ,
99
+ categories : string [ ] ,
100
+ languages : string [ ] ,
101
+ timeout : number
102
+ ) => {
103
+ this . openConnection ( ) ;
104
+ matchSocket . emit ( MATCH_REQUEST , {
105
+ user : this . user ,
106
+ complexities : complexities ,
107
+ categories : categories ,
108
+ languages : languages ,
109
+ timeout : timeout ,
110
+ } ) ;
111
+ } ;
112
+
82
113
acceptMatch = ( ) => {
83
- this . socket . emit ( MATCH_ACCEPTED , this . matchId ) ;
114
+ matchSocket . emit ( MATCH_ACCEPTED , this . matchId ) ;
84
115
} ;
85
116
86
117
declineMatch = ( ) => {
87
- this . socket . emit ( MATCH_DECLINED , this . matchId ) ;
118
+ matchSocket . emit ( MATCH_DECLINED , this . matchId ) ;
88
119
} ;
89
120
90
121
stopMatch = ( ) => {
91
122
this . closeConnection ( ) ;
92
123
} ;
93
-
94
- closeConnection = ( ) => {
95
- this . socket . removeAllListeners ( ) ;
96
- this . socket . disconnect ( ) ;
97
- } ;
98
124
}
0 commit comments