Skip to content

Commit 6a5a69b

Browse files
committed
create socke only once
1 parent dbb38ab commit 6a5a69b

3 files changed

Lines changed: 26 additions & 18 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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ interface Params {
1818
export const StudentContainer = () => {
1919
const { room } = useParams<Params>();
2020
const { log, appendToLog, setLog } = useLog();
21-
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(null);
2221

2322
const handleConnection = () => {
2423
// Connect to socket
25-
const localSocket = createSocket({
24+
const socket = createSocket({
2625
room: room,
2726
trainertoken: '',
2827
});
2928

30-
setSocket(localSocket);
31-
32-
localSocket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
29+
socket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
3330
if (msg.type) {
3431
const { type, payload } = msg;
3532

@@ -46,14 +43,14 @@ export const StudentContainer = () => {
4643
}
4744
}
4845
});
46+
getPreviousSessionContent(
47+
socket,
48+
SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT
49+
);
4950
};
5051

5152
React.useEffect(() => {
5253
handleConnection();
53-
getPreviousSessionContent(
54-
socketRef,
55-
SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT
56-
);
5754
}, []);
5855

5956
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)