1
- import {
2
- isWebSocketCloseEvent ,
3
- isWebSocketPingEvent ,
4
- WebSocket ,
5
- } from 'https://deno.land/[email protected] /ws/mod.ts' ;
1
+ import { isWebSocketCloseEvent , WebSocket } from '../common/Dependency.ts' ;
6
2
7
3
import {
8
4
GetConnections ,
@@ -15,25 +11,36 @@ import {
15
11
16
12
import { UPPERCASE_USERNAMES } from './Configuration.ts' ;
17
13
14
+ const enum MsgStatus {
15
+ OK = 'OK' ,
16
+ NOK = 'NOK' ,
17
+ INVALID = 'INVALID' ,
18
+ USERNAME_INVALID = 'USERNAME_INVALID' ,
19
+ USERNAME_IN_USE = 'USERNAME_IN_USE' ,
20
+ NOT_IN_CHAT = 'NOT_IN_CHAT' ,
21
+ ALREADY_IN_CHAT = 'ALREADY_IN_CHAT' ,
22
+ }
23
+
18
24
/**
19
25
* h: Handler
20
26
* s: Sender
21
27
* d: Data
22
- * r: Response status
28
+ * r: Message status
23
29
*/
24
30
type WSMsgJoin = { h : 'join' ; d : string } ;
25
31
type WSMsgLeave = { h : 'leave' ; d : string } ;
26
32
type WSMsgChat = { h : 'chat' ; s : string ; d : string } ;
27
33
type WSMsgGetUsers = { h : 'getUsers' } ;
28
34
type WSMessageClient = WSMsgJoin | WSMsgLeave | WSMsgChat | WSMsgGetUsers ;
29
35
30
- type WSMsgConnectResp = { h : 'connectResp' ; d : string ; r : string } ;
31
- type WSMsgJoinResp = { h : 'joinResp' ; s : string ; r : string } ;
32
- type WSMsgLeaveResp = { h : 'leaveResp' ; r : string } ;
33
- type WSMsgChatResp = { h : 'chatResp' ; d : string ; r : string } ;
36
+ type WSMsgConnectResp = { h : 'connectResp' ; d : string ; r : MsgStatus } ;
37
+ type WSMsgJoinResp = { h : 'joinResp' ; s : string ; r : MsgStatus } ;
38
+ type WSMsgLeaveResp = { h : 'leaveResp' ; r : MsgStatus } ;
39
+ type WSMsgChatResp = { h : 'chatResp' ; d : string ; r : MsgStatus } ;
34
40
type WSMsgGetUsersResp = {
35
41
h : 'getUsersResp' ;
36
42
userList : Array < string > ;
43
+ r : MsgStatus ;
37
44
} ;
38
45
type WSMessageServer =
39
46
| WSMsgConnectResp
@@ -49,48 +56,88 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
49
56
const { id : _connId , conn : _conn } = _connInfo ;
50
57
console . log ( `Socket connected! :: ${ _connId } ` ) ;
51
58
try {
52
- await RespondeConnect ( _connInfo , 'OK' ) ;
59
+ await RespondeConnect ( _connInfo , MsgStatus . OK ) ;
53
60
for await ( const event of pWebSocket ) {
54
61
if ( typeof event === 'string' ) {
55
62
const objEvent : WSMessage = JSON . parse ( event ) ;
56
63
switch ( objEvent . h ) {
57
64
case 'join' : {
58
- const _name = UPPERCASE_USERNAMES
59
- ? objEvent . d . toUpperCase ( )
60
- : objEvent . d ;
61
- if ( ! / ^ [ a - z A - Z 0 - 9 ] + $ / i. test ( _name ) ) {
62
- await RespondJoin ( _connInfo , 'Invalid username' ) ;
63
- } else if ( await FindConnByName ( _name ) ) {
65
+ if ( ! _conn . state ) {
66
+ const _name = UPPERCASE_USERNAMES
67
+ ? objEvent . d . toUpperCase ( )
68
+ : objEvent . d ;
69
+ if ( ! / ^ [ a - z A - Z 0 - 9 ] + $ / i. test ( _name ) ) {
70
+ await RespondJoin (
71
+ _connInfo ,
72
+ MsgStatus . USERNAME_INVALID
73
+ ) ;
74
+ } else if ( await FindConnByName ( _name ) ) {
75
+ await RespondJoin (
76
+ _connInfo ,
77
+ MsgStatus . USERNAME_IN_USE
78
+ ) ;
79
+ } else {
80
+ _conn . state = true ;
81
+ _conn . name = _name ;
82
+ await BroadcastJoin ( _connInfo ) ;
83
+ await RespondJoin ( _connInfo , MsgStatus . OK ) ;
84
+ }
85
+ } else {
64
86
await RespondJoin (
65
87
_connInfo ,
66
- 'Username already in use'
88
+ MsgStatus . ALREADY_IN_CHAT
67
89
) ;
68
- } else {
69
- _conn . state = true ;
70
- _conn . name = _name ;
71
- await BroadcastJoin ( _connInfo ) ;
72
- await RespondJoin ( _connInfo , 'OK' ) ;
73
90
}
74
91
break ;
75
92
}
76
93
case 'leave' : {
77
- await BroadcastLeave ( _connInfo ) ;
78
- _conn . name = '' ;
79
- _conn . state = false ;
80
- await RespondLeave ( _connInfo , 'OK' ) ;
94
+ if ( _conn . state ) {
95
+ await BroadcastLeave ( _connInfo ) ;
96
+ _conn . name = '' ;
97
+ _conn . state = false ;
98
+ await RespondLeave ( _connInfo , MsgStatus . OK ) ;
99
+ } else {
100
+ await RespondLeave (
101
+ _connInfo ,
102
+ MsgStatus . NOT_IN_CHAT
103
+ ) ;
104
+ }
81
105
break ;
82
106
}
83
107
case 'chat' : {
84
- if ( _connInfo . conn . state ) {
108
+ if ( _conn . state ) {
85
109
await BroadcastChat ( _connInfo , objEvent . d ) ;
86
- await RespondChat ( _connInfo , 'OK' , objEvent . d ) ;
110
+ await RespondChat (
111
+ _connInfo ,
112
+ MsgStatus . OK ,
113
+ objEvent . d
114
+ ) ;
87
115
} else {
88
- await RespondChat ( _connInfo , 'Invalid' , objEvent . d ) ;
116
+ await RespondChat (
117
+ _connInfo ,
118
+ MsgStatus . NOT_IN_CHAT ,
119
+ objEvent . d
120
+ ) ;
89
121
}
90
122
break ;
91
123
}
92
124
case 'getUsers' : {
93
- await RespondGetUsers ( _connInfo ) ;
125
+ if ( _conn . state ) {
126
+ const lUser = ( await GetConnections ( ) ) . map (
127
+ ( pConnection ) => pConnection . conn . name
128
+ ) ;
129
+ await RespondGetUsers (
130
+ _connInfo ,
131
+ lUser ,
132
+ MsgStatus . OK
133
+ ) ;
134
+ } else {
135
+ await RespondGetUsers (
136
+ _connInfo ,
137
+ [ ] ,
138
+ MsgStatus . NOT_IN_CHAT
139
+ ) ;
140
+ }
94
141
break ;
95
142
}
96
143
default : {
@@ -100,7 +147,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
100
147
}
101
148
} else if ( isWebSocketCloseEvent ( event ) ) {
102
149
console . log ( `Socket disconnected! :: ${ _connId } ` ) ;
103
- if ( _connInfo . conn . state ) {
150
+ if ( _conn . state ) {
104
151
await BroadcastLeave ( _connInfo ) ;
105
152
}
106
153
await RemoveConnById ( _connId ) ;
@@ -116,7 +163,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
116
163
}
117
164
}
118
165
119
- async function RespondeConnect ( pConnInfo : ConnInfo , pStatus : string ) {
166
+ async function RespondeConnect ( pConnInfo : ConnInfo , pStatus : MsgStatus ) {
120
167
const { id : _Id } = pConnInfo ;
121
168
return Respond ( pConnInfo , {
122
169
h : 'connectResp' ,
@@ -125,7 +172,7 @@ async function RespondeConnect(pConnInfo: ConnInfo, pStatus: string) {
125
172
} ) ;
126
173
}
127
174
128
- async function RespondJoin ( pConnInfo : ConnInfo , pStatus : string ) {
175
+ async function RespondJoin ( pConnInfo : ConnInfo , pStatus : MsgStatus ) {
129
176
const { id : _Id , conn : _Conn } = pConnInfo ;
130
177
const { name : _Name } = _Conn ;
131
178
return Respond ( pConnInfo , {
@@ -135,7 +182,7 @@ async function RespondJoin(pConnInfo: ConnInfo, pStatus: string) {
135
182
} ) ;
136
183
}
137
184
138
- async function RespondLeave ( pConnInfo : ConnInfo , pStatus : string ) {
185
+ async function RespondLeave ( pConnInfo : ConnInfo , pStatus : MsgStatus ) {
139
186
return Respond ( pConnInfo , {
140
187
h : 'leaveResp' ,
141
188
r : pStatus ,
@@ -144,7 +191,7 @@ async function RespondLeave(pConnInfo: ConnInfo, pStatus: string) {
144
191
145
192
async function RespondChat (
146
193
pConnInfo : ConnInfo ,
147
- pStatus : string ,
194
+ pStatus : MsgStatus ,
148
195
pChatMsg : string
149
196
) {
150
197
return Respond ( pConnInfo , {
@@ -154,12 +201,15 @@ async function RespondChat(
154
201
} ) ;
155
202
}
156
203
157
- async function RespondGetUsers ( pConnInfo : ConnInfo ) {
204
+ async function RespondGetUsers (
205
+ pConnInfo : ConnInfo ,
206
+ pListUser : Array < string > ,
207
+ pStatus : MsgStatus
208
+ ) {
158
209
return Respond ( pConnInfo , {
159
210
h : 'getUsersResp' ,
160
- userList : ( await GetConnections ( ) ) . map (
161
- ( pConnection ) => pConnection . conn . name
162
- ) ,
211
+ userList : pListUser ,
212
+ r : pStatus ,
163
213
} ) ;
164
214
}
165
215
0 commit comments