Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Commit 7e716f8

Browse files
committed
Make sure that the user is on the chat before answering requests.
Move all external dependencies to one file.
1 parent d2322ee commit 7e716f8

File tree

6 files changed

+99
-47
lines changed

6 files changed

+99
-47
lines changed

src/client/Client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServerRequest } from 'https://deno.land/[email protected]/http/server.ts';
1+
import { ServerRequest } from '../common/Dependency.ts';
22

33
const PUBLIC_PATH = `${Deno.cwd()}/src/client/public`;
44

src/common/Dependency.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from 'https://deno.land/[email protected]/uuid/mod.ts';
2+
3+
export * from 'https://deno.land/[email protected]/http/server.ts';
4+
5+
export * from 'https://deno.land/[email protected]/ws/mod.ts';

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
2-
import { acceptable } from 'https://deno.land/[email protected]/ws/mod.ts';
1+
import { serve, acceptable } from './common/Dependency.ts';
32
import { HandleServer } from './server/Server.ts';
43
import { HandleClient } from './client/Client.ts';
54

src/server/Connections.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { WebSocket } from 'https://deno.land/[email protected]/ws/mod.ts';
2-
import { v4 } from 'https://deno.land/[email protected]/uuid/mod.ts';
1+
import { WebSocket, v4 } from '../common/Dependency.ts';
32

43
export type Connection = { ws: WebSocket; state: boolean; name: string };
54
export type ConnInfo = { id: string; conn: Connection };

src/server/Server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { acceptWebSocket } from 'https://deno.land/[email protected]/ws/mod.ts';
2-
import { ServerRequest } from 'https://deno.land/[email protected]/http/server.ts';
1+
import { acceptWebSocket, ServerRequest } from '../common/Dependency.ts';
32
import { HandleWSConn } from './WebSocket.ts';
43

54
export async function HandleServer(pRequest: ServerRequest): Promise<void> {

src/server/WebSocket.ts

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
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';
62

73
import {
84
GetConnections,
@@ -15,25 +11,36 @@ import {
1511

1612
import { UPPERCASE_USERNAMES } from './Configuration.ts';
1713

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+
1824
/**
1925
* h: Handler
2026
* s: Sender
2127
* d: Data
22-
* r: Response status
28+
* r: Message status
2329
*/
2430
type WSMsgJoin = { h: 'join'; d: string };
2531
type WSMsgLeave = { h: 'leave'; d: string };
2632
type WSMsgChat = { h: 'chat'; s: string; d: string };
2733
type WSMsgGetUsers = { h: 'getUsers' };
2834
type WSMessageClient = WSMsgJoin | WSMsgLeave | WSMsgChat | WSMsgGetUsers;
2935

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 };
3440
type WSMsgGetUsersResp = {
3541
h: 'getUsersResp';
3642
userList: Array<string>;
43+
r: MsgStatus;
3744
};
3845
type WSMessageServer =
3946
| WSMsgConnectResp
@@ -49,48 +56,88 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
4956
const { id: _connId, conn: _conn } = _connInfo;
5057
console.log(`Socket connected! :: ${_connId}`);
5158
try {
52-
await RespondeConnect(_connInfo, 'OK');
59+
await RespondeConnect(_connInfo, MsgStatus.OK);
5360
for await (const event of pWebSocket) {
5461
if (typeof event === 'string') {
5562
const objEvent: WSMessage = JSON.parse(event);
5663
switch (objEvent.h) {
5764
case 'join': {
58-
const _name = UPPERCASE_USERNAMES
59-
? objEvent.d.toUpperCase()
60-
: objEvent.d;
61-
if (!/^[a-zA-Z0-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-zA-Z0-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 {
6486
await RespondJoin(
6587
_connInfo,
66-
'Username already in use'
88+
MsgStatus.ALREADY_IN_CHAT
6789
);
68-
} else {
69-
_conn.state = true;
70-
_conn.name = _name;
71-
await BroadcastJoin(_connInfo);
72-
await RespondJoin(_connInfo, 'OK');
7390
}
7491
break;
7592
}
7693
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+
}
81105
break;
82106
}
83107
case 'chat': {
84-
if (_connInfo.conn.state) {
108+
if (_conn.state) {
85109
await BroadcastChat(_connInfo, objEvent.d);
86-
await RespondChat(_connInfo, 'OK', objEvent.d);
110+
await RespondChat(
111+
_connInfo,
112+
MsgStatus.OK,
113+
objEvent.d
114+
);
87115
} else {
88-
await RespondChat(_connInfo, 'Invalid', objEvent.d);
116+
await RespondChat(
117+
_connInfo,
118+
MsgStatus.NOT_IN_CHAT,
119+
objEvent.d
120+
);
89121
}
90122
break;
91123
}
92124
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+
}
94141
break;
95142
}
96143
default: {
@@ -100,7 +147,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
100147
}
101148
} else if (isWebSocketCloseEvent(event)) {
102149
console.log(`Socket disconnected! :: ${_connId}`);
103-
if (_connInfo.conn.state) {
150+
if (_conn.state) {
104151
await BroadcastLeave(_connInfo);
105152
}
106153
await RemoveConnById(_connId);
@@ -116,7 +163,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
116163
}
117164
}
118165

119-
async function RespondeConnect(pConnInfo: ConnInfo, pStatus: string) {
166+
async function RespondeConnect(pConnInfo: ConnInfo, pStatus: MsgStatus) {
120167
const { id: _Id } = pConnInfo;
121168
return Respond(pConnInfo, {
122169
h: 'connectResp',
@@ -125,7 +172,7 @@ async function RespondeConnect(pConnInfo: ConnInfo, pStatus: string) {
125172
});
126173
}
127174

128-
async function RespondJoin(pConnInfo: ConnInfo, pStatus: string) {
175+
async function RespondJoin(pConnInfo: ConnInfo, pStatus: MsgStatus) {
129176
const { id: _Id, conn: _Conn } = pConnInfo;
130177
const { name: _Name } = _Conn;
131178
return Respond(pConnInfo, {
@@ -135,7 +182,7 @@ async function RespondJoin(pConnInfo: ConnInfo, pStatus: string) {
135182
});
136183
}
137184

138-
async function RespondLeave(pConnInfo: ConnInfo, pStatus: string) {
185+
async function RespondLeave(pConnInfo: ConnInfo, pStatus: MsgStatus) {
139186
return Respond(pConnInfo, {
140187
h: 'leaveResp',
141188
r: pStatus,
@@ -144,7 +191,7 @@ async function RespondLeave(pConnInfo: ConnInfo, pStatus: string) {
144191

145192
async function RespondChat(
146193
pConnInfo: ConnInfo,
147-
pStatus: string,
194+
pStatus: MsgStatus,
148195
pChatMsg: string
149196
) {
150197
return Respond(pConnInfo, {
@@ -154,12 +201,15 @@ async function RespondChat(
154201
});
155202
}
156203

157-
async function RespondGetUsers(pConnInfo: ConnInfo) {
204+
async function RespondGetUsers(
205+
pConnInfo: ConnInfo,
206+
pListUser: Array<string>,
207+
pStatus: MsgStatus
208+
) {
158209
return Respond(pConnInfo, {
159210
h: 'getUsersResp',
160-
userList: (await GetConnections()).map(
161-
(pConnection) => pConnection.conn.name
162-
),
211+
userList: pListUser,
212+
r: pStatus,
163213
});
164214
}
165215

0 commit comments

Comments
 (0)