Skip to content

Commit ca1acb5

Browse files
committed
bug/fix-duplicated-room
1 parent 135fc1e commit ca1acb5

3 files changed

Lines changed: 160 additions & 93 deletions

File tree

‎back/src/pods/messages/messages.socket-events.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import SocketIOClient, { Socket } from 'socket.io';
22
import { Action, SocketInfo } from './messages.model';
3-
import { processInputMessage, processOutputMessageCollection } from './processors';
3+
import {
4+
processInputMessage,
5+
processOutputMessageCollection,
6+
} from './processors';
47
import { InputMessageTypes } from './messages.consts';
58

69
export const messageSocketEvents = async (
@@ -44,7 +47,7 @@ export const messageSocketEvents = async (
4447
processOutputMessageCollection(socketInfo, outputMessageCollection);
4548

4649
socket.on('message', async function (message: any) {
47-
console.log("socket.on 'message':: " + message);
50+
console.log(`socket.on 'message': ${JSON.stringify(message)}`);
4851
if (message && message.type) {
4952
const outputMessageCollection: Action[] = await processInputMessage(
5053
socketInfo,
@@ -54,5 +57,4 @@ export const messageSocketEvents = async (
5457
await processOutputMessageCollection(socketInfo, outputMessageCollection);
5558
}
5659
});
57-
5860
};
Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { sessionRepository, roomRepository, RoomInfo } from 'dals';
12
import { InputMessageTypes, OutputMessageTypes } from '../messages.consts';
2-
import { Action, InputEstablishConnectionTrainer, SocketInfo } from '../messages.model';
3-
import {sessionRepository, roomRepository } from 'dals';
4-
const {addNewUser, getRoomFromConnectionId, isExistingConnection, isTrainerUser } = sessionRepository;
5-
const { isRoomAvailable, saveRoomInfo} = roomRepository;
3+
import {
4+
Action,
5+
InputEstablishConnectionTrainer,
6+
SocketInfo,
7+
} from '../messages.model';
68

7-
export const processInputMessage = async (socketInfo: SocketInfo, action: Action): Promise<Action[]> => {
9+
export const processInputMessage = async (
10+
socketInfo: SocketInfo,
11+
action: Action
12+
): Promise<Action[]> => {
813
let outputActionCollection: Action[] = [];
914
switch (action.type) {
1015
case InputMessageTypes.ESTABLISH_CONNECTION_TRAINER:
@@ -37,10 +42,10 @@ export const processInputMessage = async (socketInfo: SocketInfo, action: Action
3742
);
3843
break;
3944
case InputMessageTypes.STUDENT_REQUEST_FULL_CONTENT:
40-
outputActionCollection = await handleRequestGetStudentContent(socketInfo);
45+
outputActionCollection = await handleRequestGetStudentContent();
4146
break;
4247
case InputMessageTypes.TRAINER_REQUEST_FULL_CONTENT:
43-
outputActionCollection = await handleRequestGetTrainerContent(socketInfo);
48+
outputActionCollection = await handleRequestGetTrainerContent();
4449
break;
4550
default:
4651
break;
@@ -49,63 +54,92 @@ export const processInputMessage = async (socketInfo: SocketInfo, action: Action
4954
return outputActionCollection;
5055
};
5156

52-
const handleRequestGetStudentContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
53-
return [{ type: OutputMessageTypes.STUDENT_SEND_FULL_CONTENT }];
54-
};
57+
const handleEstablishConnectionTrainer = async (
58+
socketInfo: SocketInfo,
59+
room: string,
60+
trainerToken: string
61+
): Promise<Action[]> => {
62+
if (!trainerToken || !room) {
63+
// Ignore
64+
return [];
65+
}
5566

56-
const handleRequestGetTrainerContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
57-
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
58-
};
67+
const roomAvailable: boolean = await roomRepository.isRoomAvailable(room);
5968

60-
const handleTrainerSendText = async (socketInfo: SocketInfo, text: string, action: string): Promise<Action[]> => {
61-
if (!isTrainerUser(socketInfo.connectionId)) {
62-
return [];
69+
if (roomAvailable) {
70+
roomRepository.saveRoomInfo({ room, content: '' }, false);
6371
}
64-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
65-
const roomInfo = { room, content: text };
66-
67-
switch(action){
68-
case InputMessageTypes.TRAINER_APPEND_TEXT:
69-
saveRoomInfo(roomInfo, false);
70-
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
71-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
72-
saveRoomInfo(roomInfo, true);
73-
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT}];
74-
default:
75-
return [];
76-
};
72+
73+
if (
74+
roomAvailable ||
75+
!(await sessionRepository.isExistingConnection(socketInfo.connectionId))
76+
) {
77+
await sessionRepository.addNewUser({
78+
connectionId: socketInfo.connectionId,
79+
room,
80+
trainerToken,
81+
isTrainer: !!trainerToken,
82+
});
83+
socketInfo.socket.join(room);
84+
}
85+
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER }];
7786
};
7887

79-
const handleEstablishConnectionStudent = async (socketInfo: SocketInfo, room: string): Promise<Action[]> => {
88+
const handleEstablishConnectionStudent = async (
89+
socketInfo: SocketInfo,
90+
room: string
91+
): Promise<Action[]> => {
8092
if (!room) {
8193
// Ignore
8294
return [];
8395
}
8496

85-
const roomAvailable: boolean = await isRoomAvailable(room);
97+
const roomAvailable: boolean = await roomRepository.isRoomAvailable(room);
8698

87-
if (roomAvailable || !(await isExistingConnection(socketInfo.connectionId))) {
88-
await addNewUser({connectionId: socketInfo.connectionId, room, trainerToken: '', isTrainer: false});
99+
if (
100+
roomAvailable ||
101+
!(await sessionRepository.isExistingConnection(socketInfo.connectionId))
102+
) {
103+
await sessionRepository.addNewUser({
104+
connectionId: socketInfo.connectionId,
105+
room,
106+
trainerToken: '',
107+
isTrainer: false,
108+
});
89109
socketInfo.socket.join(room);
90110
}
91111
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_STUDENT }];
92112
};
93113

94-
const handleEstablishConnectionTrainer = async (socketInfo: SocketInfo, room: string, trainerToken: string): Promise<Action[]> => {
95-
if (!trainerToken || !room) {
96-
// Ignore
114+
const handleTrainerSendText = async (
115+
socketInfo: SocketInfo,
116+
text: string,
117+
action: string
118+
): Promise<Action[]> => {
119+
if (!sessionRepository.isTrainerUser(socketInfo.connectionId)) {
97120
return [];
98121
}
122+
const room = await sessionRepository.getRoomFromConnectionId(
123+
socketInfo.connectionId
124+
);
125+
const roomInfo: RoomInfo = { room, content: text };
99126

100-
const roomAvailable: boolean = await isRoomAvailable(room);
101-
102-
if(roomAvailable) {
103-
saveRoomInfo({room, content:""}, false);
127+
switch (action) {
128+
case InputMessageTypes.TRAINER_APPEND_TEXT:
129+
await roomRepository.saveRoomInfo(roomInfo, false);
130+
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
131+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
132+
await roomRepository.saveRoomInfo(roomInfo, true);
133+
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT }];
134+
default:
135+
return [];
104136
}
137+
};
105138

106-
if (roomAvailable || !(await isExistingConnection(socketInfo.connectionId))) {
107-
await addNewUser({ connectionId: socketInfo.connectionId, room, trainerToken, isTrainer: !!trainerToken });
108-
socketInfo.socket.join(room);
109-
}
110-
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER }];
111-
};
139+
const handleRequestGetStudentContent = async (): Promise<Action[]> => {
140+
return [{ type: OutputMessageTypes.STUDENT_SEND_FULL_CONTENT }];
141+
};
142+
143+
const handleRequestGetTrainerContent = async (): Promise<Action[]> => {
144+
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
145+
};
Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import {OutputMessageTypes, SocketOuputMessageLiteral} from '../messages.consts';
2-
import { Action, SocketInfo } from '../messages.model';
31
import { sessionRepository, roomRepository } from 'dals';
4-
const { isTrainerUser, getRoomFromConnectionId } = sessionRepository;
5-
const { getRoomContent } = roomRepository;
6-
2+
import {
3+
OutputMessageTypes,
4+
SocketOuputMessageLiteral,
5+
} from '../messages.consts';
6+
import { Action, SocketInfo } from '../messages.model';
77
import { ResponseBase, responseType } from './response';
88

9-
export const processOutputMessageCollection = async (socketInfo: SocketInfo, actionCollection: Action[]): Promise<void> => {
9+
export const processOutputMessageCollection = async (
10+
socketInfo: SocketInfo,
11+
actionCollection: Action[]
12+
): Promise<void> => {
1013
if (actionCollection) {
1114
// TODO: Error handling
12-
await actionCollection.forEach((action) =>
13-
processOuputMessage(socketInfo, action)
14-
);
15+
for (const action of actionCollection) {
16+
await processOuputMessage(socketInfo, action);
17+
}
1518
}
1619
};
1720

18-
export const processOuputMessage = async (socketInfo: SocketInfo, action: Action) => {
19-
const { connectionId, io, socket } = socketInfo;
20-
const isMaster = await isTrainerUser(connectionId);
21-
const room = await getRoomFromConnectionId(connectionId);
22-
21+
export const processOuputMessage = async (
22+
socketInfo: SocketInfo,
23+
action: Action
24+
) => {
2325
switch (action.type) {
2426
case OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER:
25-
await handleNotifyConnectionEstablishedTrainer(socketInfo, connectionId);
27+
await handleNotifyConnectionEstablishedTrainer(socketInfo);
2628
break;
2729
case OutputMessageTypes.CONNECTION_ESTABLISHED_STUDENT:
28-
await handleNotifyConnectionEstablishedStudent(socketInfo, connectionId);
30+
await handleNotifyConnectionEstablishedStudent(socketInfo);
2931
break;
3032
case OutputMessageTypes.APPEND_TEXT:
3133
await handleAppendText(socketInfo, action.payload);
@@ -44,44 +46,73 @@ export const processOuputMessage = async (socketInfo: SocketInfo, action: Action
4446
}
4547
};
4648

47-
const handleSingleStudentSendFullContent = async (socketInfo: SocketInfo) => {
48-
await handleSendFullContent(socketInfo, responseType.STUDENT_GET_FULL_CONTENT, false);
49-
}
50-
51-
const handleTrainerSendFullContent = async (socketInfo: SocketInfo) => {
52-
await handleSendFullContent(socketInfo, responseType.TRAINER_GET_FULL_CONTENT, false);
53-
}
54-
55-
const handleUpdateFullContent = async (socketInfo: SocketInfo) => {
56-
await handleSendFullContent(socketInfo, responseType.UPDATE_FULL_CONTENT, true);
57-
}
49+
const handleNotifyConnectionEstablishedTrainer = async (
50+
socketInfo: SocketInfo
51+
) => {
52+
const room = await sessionRepository.getRoomFromConnectionId(
53+
socketInfo.connectionId
54+
);
55+
const response: ResponseBase = { type: responseType.CONNECTION_ACK };
56+
socketInfo.socket.in(room).emit(SocketOuputMessageLiteral.MESSAGE, response);
57+
};
5858

59-
const handleSendFullContent = async (socketInfo: SocketInfo, responseType: string, sendToAll: boolean) => {
60-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
61-
const content = await getRoomContent(room);
62-
const msg = {type: responseType, payload: content};
63-
sendToAll ? socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, msg) :
64-
socketInfo.socket.emit(SocketOuputMessageLiteral.MESSAGE, msg);
59+
const handleNotifyConnectionEstablishedStudent = async (
60+
socketInfo: SocketInfo
61+
) => {
62+
const room = await sessionRepository.getRoomFromConnectionId(
63+
socketInfo.connectionId
64+
);
65+
const response: ResponseBase = {
66+
type: responseType.CONNECTION_ACK,
67+
};
68+
socketInfo.socket.in(room).emit(SocketOuputMessageLiteral.MESSAGE, response);
6569
};
6670

6771
const handleAppendText = async (socketInfo: SocketInfo, text: string) => {
68-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
72+
const room = await sessionRepository.getRoomFromConnectionId(
73+
socketInfo.connectionId
74+
);
6975
socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, {
7076
type: responseType.APPEND_TEXT,
7177
payload: text,
7278
});
7379
};
7480

75-
const handleNotifyConnectionEstablishedTrainer = async (socketInfo: SocketInfo, connectionId: string) => {
76-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
77-
const response: ResponseBase = { type: responseType.CONNECTION_ACK };
78-
socketInfo.socket.in(room).emit(SocketOuputMessageLiteral.MESSAGE, response);
81+
const handleUpdateFullContent = async (socketInfo: SocketInfo) => {
82+
await handleSendFullContent(
83+
socketInfo,
84+
responseType.UPDATE_FULL_CONTENT,
85+
true
86+
);
7987
};
8088

81-
const handleNotifyConnectionEstablishedStudent = async (socketInfo: SocketInfo, connectionId: string) => {
82-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
83-
const response: ResponseBase = {
84-
type: responseType.CONNECTION_ACK,
85-
};
86-
socketInfo.socket.in(room).emit(SocketOuputMessageLiteral.MESSAGE, response);
89+
const handleSingleStudentSendFullContent = async (socketInfo: SocketInfo) => {
90+
await handleSendFullContent(
91+
socketInfo,
92+
responseType.STUDENT_GET_FULL_CONTENT,
93+
false
94+
);
95+
};
96+
97+
const handleTrainerSendFullContent = async (socketInfo: SocketInfo) => {
98+
await handleSendFullContent(
99+
socketInfo,
100+
responseType.TRAINER_GET_FULL_CONTENT,
101+
false
102+
);
103+
};
104+
105+
const handleSendFullContent = async (
106+
socketInfo: SocketInfo,
107+
responseType: string,
108+
sendToAll: boolean
109+
) => {
110+
const room = await sessionRepository.getRoomFromConnectionId(
111+
socketInfo.connectionId
112+
);
113+
const content = await roomRepository.getRoomContent(room);
114+
const msg = { type: responseType, payload: content };
115+
sendToAll
116+
? socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, msg)
117+
: socketInfo.socket.emit(SocketOuputMessageLiteral.MESSAGE, msg);
87118
};

0 commit comments

Comments
 (0)