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

Commit a28a8a4

Browse files
committed
fix conflict
2 parents 6fe01b4 + 7e716f8 commit a28a8a4

File tree

6 files changed

+98
-47
lines changed

6 files changed

+98
-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/std@0.76.0/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/std@0.76.0/uuid/mod.ts';
2+
3+
export * from 'https://deno.land/std@0.76.0/http/server.ts';
4+
5+
export * from 'https://deno.land/std@0.76.0/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/std@0.76.0/http/server.ts';
2-
import { acceptable } from 'https://deno.land/std@0.76.0/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/std@0.76.0/ws/mod.ts';
2-
import { v4 } from 'https://deno.land/std@0.76.0/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/std@0.76.0/ws/mod.ts';
2-
import { ServerRequest } from 'https://deno.land/std@0.76.0/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: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { v4 } from 'https://deno.land/std@0.76.0/uuid/mod.ts';
2-
import {
3-
isWebSocketCloseEvent,
4-
isWebSocketPingEvent,
5-
WebSocket,
6-
} from 'https://deno.land/std@0.76.0/ws/mod.ts';
2+
import { isWebSocketCloseEvent, WebSocket } from '../common/Dependency.ts';
73

84
import {
95
GetConnections,
@@ -18,12 +14,21 @@ import { UPPERCASE_USERNAMES } from './Configuration.ts';
1814

1915
type FileInfo = { data: string; name: string };
2016
const UserFiles: Map<string, FileInfo> = new Map();
17+
const enum MsgStatus {
18+
OK = 'OK',
19+
NOK = 'NOK',
20+
INVALID = 'INVALID',
21+
USERNAME_INVALID = 'USERNAME_INVALID',
22+
USERNAME_IN_USE = 'USERNAME_IN_USE',
23+
NOT_IN_CHAT = 'NOT_IN_CHAT',
24+
ALREADY_IN_CHAT = 'ALREADY_IN_CHAT',
25+
}
2126

2227
/**
2328
* h: Handler
2429
* s: Sender
2530
* d: Data
26-
* r: Response status
31+
* r: Message status
2732
*/
2833
type WSMsgJoin = { h: 'join'; d: string };
2934
type WSMsgLeave = { h: 'leave'; d: string };
@@ -40,13 +45,14 @@ type WSMessageClient =
4045
| WSMsgGetUsers
4146
| WSMsgSendFiles;
4247

43-
type WSMsgConnectResp = { h: 'connectResp'; d: string; r: string };
44-
type WSMsgJoinResp = { h: 'joinResp'; s: string; r: string };
45-
type WSMsgLeaveResp = { h: 'leaveResp'; r: string };
46-
type WSMsgChatResp = { h: 'chatResp'; d: string; r: string };
48+
type WSMsgConnectResp = { h: 'connectResp'; d: string; r: MsgStatus };
49+
type WSMsgJoinResp = { h: 'joinResp'; s: string; r: MsgStatus };
50+
type WSMsgLeaveResp = { h: 'leaveResp'; r: MsgStatus };
51+
type WSMsgChatResp = { h: 'chatResp'; d: string; r: MsgStatus };
4752
type WSMsgGetUsersResp = {
4853
h: 'getUsersResp';
4954
userList: Array<string>;
55+
r: MsgStatus;
5056
};
5157
type WSMsgSendFilesResp = {
5258
h: 'sendFilesResp';
@@ -67,48 +73,88 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
6773
const { id: _connId, conn: _conn } = _connInfo;
6874
console.log(`Socket connected! :: ${_connId}`);
6975
try {
70-
await RespondeConnect(_connInfo, 'OK');
76+
await RespondeConnect(_connInfo, MsgStatus.OK);
7177
for await (const event of pWebSocket) {
7278
if (typeof event === 'string') {
7379
const objEvent: WSMessage = JSON.parse(event);
7480
switch (objEvent.h) {
7581
case 'join': {
76-
const _name = UPPERCASE_USERNAMES
77-
? objEvent.d.toUpperCase()
78-
: objEvent.d;
79-
if (!/^[a-zA-Z0-9]+$/i.test(_name)) {
80-
await RespondJoin(_connInfo, 'Invalid username');
81-
} else if (await FindConnByName(_name)) {
82+
if (!_conn.state) {
83+
const _name = UPPERCASE_USERNAMES
84+
? objEvent.d.toUpperCase()
85+
: objEvent.d;
86+
if (!/^[a-zA-Z0-9]+$/i.test(_name)) {
87+
await RespondJoin(
88+
_connInfo,
89+
MsgStatus.USERNAME_INVALID
90+
);
91+
} else if (await FindConnByName(_name)) {
92+
await RespondJoin(
93+
_connInfo,
94+
MsgStatus.USERNAME_IN_USE
95+
);
96+
} else {
97+
_conn.state = true;
98+
_conn.name = _name;
99+
await BroadcastJoin(_connInfo);
100+
await RespondJoin(_connInfo, MsgStatus.OK);
101+
}
102+
} else {
82103
await RespondJoin(
83104
_connInfo,
84-
'Username already in use'
105+
MsgStatus.ALREADY_IN_CHAT
85106
);
86-
} else {
87-
_conn.state = true;
88-
_conn.name = _name;
89-
await BroadcastJoin(_connInfo);
90-
await RespondJoin(_connInfo, 'OK');
91107
}
92108
break;
93109
}
94110
case 'leave': {
95-
await BroadcastLeave(_connInfo);
96-
_conn.name = '';
97-
_conn.state = false;
98-
await RespondLeave(_connInfo, 'OK');
111+
if (_conn.state) {
112+
await BroadcastLeave(_connInfo);
113+
_conn.name = '';
114+
_conn.state = false;
115+
await RespondLeave(_connInfo, MsgStatus.OK);
116+
} else {
117+
await RespondLeave(
118+
_connInfo,
119+
MsgStatus.NOT_IN_CHAT
120+
);
121+
}
99122
break;
100123
}
101124
case 'chat': {
102-
if (_connInfo.conn.state) {
125+
if (_conn.state) {
103126
await BroadcastChat(_connInfo, objEvent.d);
104-
await RespondChat(_connInfo, 'OK', objEvent.d);
127+
await RespondChat(
128+
_connInfo,
129+
MsgStatus.OK,
130+
objEvent.d
131+
);
105132
} else {
106-
await RespondChat(_connInfo, 'Invalid', objEvent.d);
133+
await RespondChat(
134+
_connInfo,
135+
MsgStatus.NOT_IN_CHAT,
136+
objEvent.d
137+
);
107138
}
108139
break;
109140
}
110141
case 'getUsers': {
111-
await RespondGetUsers(_connInfo);
142+
if (_conn.state) {
143+
const lUser = (await GetConnections()).map(
144+
(pConnection) => pConnection.conn.name
145+
);
146+
await RespondGetUsers(
147+
_connInfo,
148+
lUser,
149+
MsgStatus.OK
150+
);
151+
} else {
152+
await RespondGetUsers(
153+
_connInfo,
154+
[],
155+
MsgStatus.NOT_IN_CHAT
156+
);
157+
}
112158
break;
113159
}
114160
case 'sendFiles': {
@@ -123,7 +169,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
123169
}
124170
} else if (isWebSocketCloseEvent(event)) {
125171
console.log(`Socket disconnected! :: ${_connId}`);
126-
if (_connInfo.conn.state) {
172+
if (_conn.state) {
127173
await BroadcastLeave(_connInfo);
128174
}
129175
await RemoveConnById(_connId);
@@ -139,7 +185,7 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
139185
}
140186
}
141187

142-
async function RespondeConnect(pConnInfo: ConnInfo, pStatus: string) {
188+
async function RespondeConnect(pConnInfo: ConnInfo, pStatus: MsgStatus) {
143189
const { id: _Id } = pConnInfo;
144190
return Respond(pConnInfo, {
145191
h: 'connectResp',
@@ -148,7 +194,7 @@ async function RespondeConnect(pConnInfo: ConnInfo, pStatus: string) {
148194
});
149195
}
150196

151-
async function RespondJoin(pConnInfo: ConnInfo, pStatus: string) {
197+
async function RespondJoin(pConnInfo: ConnInfo, pStatus: MsgStatus) {
152198
const { id: _Id, conn: _Conn } = pConnInfo;
153199
const { name: _Name } = _Conn;
154200
return Respond(pConnInfo, {
@@ -158,7 +204,7 @@ async function RespondJoin(pConnInfo: ConnInfo, pStatus: string) {
158204
});
159205
}
160206

161-
async function RespondLeave(pConnInfo: ConnInfo, pStatus: string) {
207+
async function RespondLeave(pConnInfo: ConnInfo, pStatus: MsgStatus) {
162208
return Respond(pConnInfo, {
163209
h: 'leaveResp',
164210
r: pStatus,
@@ -167,7 +213,7 @@ async function RespondLeave(pConnInfo: ConnInfo, pStatus: string) {
167213

168214
async function RespondChat(
169215
pConnInfo: ConnInfo,
170-
pStatus: string,
216+
pStatus: MsgStatus,
171217
pChatMsg: string
172218
) {
173219
return Respond(pConnInfo, {
@@ -177,12 +223,15 @@ async function RespondChat(
177223
});
178224
}
179225

180-
async function RespondGetUsers(pConnInfo: ConnInfo) {
226+
async function RespondGetUsers(
227+
pConnInfo: ConnInfo,
228+
pListUser: Array<string>,
229+
pStatus: MsgStatus
230+
) {
181231
return Respond(pConnInfo, {
182232
h: 'getUsersResp',
183-
userList: (await GetConnections()).map(
184-
(pConnection) => pConnection.conn.name
185-
),
233+
userList: pListUser,
234+
r: pStatus,
186235
});
187236
}
188237

0 commit comments

Comments
 (0)