Skip to content

Commit e0ead48

Browse files
authored
Merge pull request #133 from Lemoncode/bug/fix-duplicated-room
create socke only once
2 parents 0b68577 + 6dbdc0d commit e0ead48

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SocketOuputMessageLiteral, SocketEmitMessageTypes } from 'core';
22

3-
export const getPreviousSessionContent = (socketRef: React.MutableRefObject<SocketIO.Socket>, messageType: string) => {
4-
socketRef.current.emit(SocketOuputMessageLiteral.MESSAGE, {
3+
export const getPreviousSessionContent = (socket:SocketIO.Socket, messageType: string) => {
4+
socket.emit(SocketOuputMessageLiteral.MESSAGE, {
55
type: messageType,
66
});
7-
};
7+
};

‎front/src/pods/student/student.container.tsx‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from 'core';
88
import { useLog } from 'core';
99
import { StudentComponent } from './student.component';
10-
import { useWithRef } from 'common';
1110
import { getPreviousSessionContent } from 'common-app';
1211
import { useParams } from 'react-router-dom';
1312

@@ -18,18 +17,15 @@ interface Params {
1817
export const StudentContainer = () => {
1918
const { room } = useParams<Params>();
2019
const { log, appendToLog, setLog } = useLog();
21-
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(null);
2220

2321
const handleConnection = () => {
2422
// Connect to socket
25-
const localSocket = createSocket({
23+
const socket = createSocket({
2624
room: room,
2725
trainertoken: '',
2826
});
2927

30-
setSocket(localSocket);
31-
32-
localSocket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
28+
socket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
3329
if (msg.type) {
3430
const { type, payload } = msg;
3531

@@ -46,14 +42,14 @@ export const StudentContainer = () => {
4642
}
4743
}
4844
});
45+
getPreviousSessionContent(
46+
socket,
47+
SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT
48+
);
4949
};
5050

5151
React.useEffect(() => {
5252
handleConnection();
53-
getPreviousSessionContent(
54-
socketRef,
55-
SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT
56-
);
5753
}, []);
5854

5955
return (

‎front/src/pods/trainer/trainer.container.tsx‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ interface Params {
2121
export const TrainerContainer = () => {
2222
const { token, room } = useParams<Params>();
2323
const { log, appendToLog, setLog } = useLog();
24-
const socketDetails = { room: room, trainertoken: token };
25-
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(createSocket(socketDetails));
24+
const [_, setSocket, socketRef] = useWithRef<SocketIO.Socket>(null);
2625

2726
const [currentTrainerUrl, setCurrentTrainerUrl] = React.useState<string>('');
2827
const [currentStudentUrl, setCurrentStudentUrl] = React.useState<string>('');
2928

3029
const handleConnection = () => {
30+
const socket = createSocket({ room: room, trainertoken: token });
31+
setSocket(socket);
3132
socket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
3233
if (msg.type) {
3334
const { type, payload } = msg;
@@ -45,19 +46,22 @@ export const TrainerContainer = () => {
4546
}
4647
}
4748
});
49+
getPreviousSessionContent(
50+
socket,
51+
SocketEmitMessageTypes.TRAINER_REQUEST_FULL_CONTENT
52+
);
4853
};
4954

5055
React.useEffect(() => {
5156
setCurrentTrainerUrl(`${getHostBaseUrl()}#${routes.trainer(room, token)}`);
5257
setCurrentStudentUrl(`${getHostBaseUrl()}#${routes.student(room)}`);
5358
handleConnection();
54-
getPreviousSessionContent(socketRef, SocketEmitMessageTypes.TRAINER_REQUEST_FULL_CONTENT);
5559
}, []);
5660

5761
const appendLineSeparator = (text: string): string =>
5862
`${text}${lineSeparator}`;
5963

60-
const sendTrainerTextToServer = (text: string, action:string): void => {
64+
const sendTrainerTextToServer = (text: string, action: string): void => {
6165
socketRef.current.emit(SocketOuputMessageLiteral.MESSAGE, {
6266
type: action,
6367
payload: text,
@@ -66,11 +70,18 @@ export const TrainerContainer = () => {
6670

6771
const handleAppendTrainerText = (trainerText: string): void => {
6872
const finalText = appendLineSeparator(trainerText);
69-
sendTrainerTextToServer(finalText, SocketEmitMessageTypes.TRAINER_APPEND_TEXT);
73+
sendTrainerTextToServer(
74+
finalText,
75+
SocketEmitMessageTypes.TRAINER_APPEND_TEXT
76+
);
7077
};
7178

7279
const handleSendFullContentLog = (fullContent: string): void => {
73-
fullContent && sendTrainerTextToServer(fullContent, SocketEmitMessageTypes.TRAINER_SET_FULL_TEXT);
80+
fullContent &&
81+
sendTrainerTextToServer(
82+
fullContent,
83+
SocketEmitMessageTypes.TRAINER_SET_FULL_TEXT
84+
);
7485
};
7586

7687
return (

0 commit comments

Comments
 (0)