Skip to content

Commit bd320d3

Browse files
committed
Close socket connection after a delay
1 parent 0a77cbf commit bd320d3

File tree

11 files changed

+53
-18
lines changed

11 files changed

+53
-18
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Socket } from "socket.io";
2+
import { UserConnection } from "../utils/types";
3+
4+
const userConnections: Map<string, UserConnection> = new Map();
5+
6+
const delay = 3000;
7+
8+
export const disconnectUser = (socket: Socket) => {
9+
const { username } = socket.data;
10+
const userConnection = userConnections.get(username);
11+
clearTimeout(userConnection?.timeout);
12+
const timeout = setTimeout(() => {
13+
console.log("DISCONNECTING: ", socket.data);
14+
userConnections.delete(username);
15+
socket.disconnect();
16+
}, delay);
17+
userConnections.set(username, { timeout });
18+
};
19+
20+
export const connectUser = (username: string) => {
21+
console.log("CONNECTING: ", username);
22+
const userConnection = userConnections.get(username);
23+
clearTimeout(userConnection?.timeout);
24+
userConnections.set(username, {});
25+
};

backend/communication-service/src/handlers/websocketHandler.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import { Socket } from "socket.io";
22
import { CommunicationEvents, MessageTypes } from "../utils/types";
33
import { BOT_NAME } from "../utils/constants";
44
import { io } from "../server";
5+
import { connectUser, disconnectUser } from "./userConnectionHandler";
56

67
export const handleWebsocketCommunicationEvents = (socket: Socket) => {
78
socket.on(
89
CommunicationEvents.JOIN,
910
async ({ roomId, username }: { roomId: string; username: string }) => {
10-
if (!roomId) {
11-
return;
12-
}
13-
11+
connectUser(username);
1412
const room = io.sockets.adapter.rooms.get(roomId);
1513
if (room?.has(socket.id)) {
1614
socket.emit(CommunicationEvents.ALREADY_JOINED);
@@ -55,10 +53,13 @@ export const handleWebsocketCommunicationEvents = (socket: Socket) => {
5553
}
5654
);
5755

56+
socket.on(CommunicationEvents.USER_DISCONNECT, () => {
57+
disconnectUser(socket);
58+
});
59+
5860
socket.on(CommunicationEvents.DISCONNECT, () => {
5961
const { roomId } = socket.data;
6062
if (roomId) {
61-
console.log("disconnected", roomId, socket.data.username);
6263
const createdTime = Date.now();
6364
socket.to(roomId).emit(CommunicationEvents.DISCONNECTED, {
6465
from: BOT_NAME,

backend/communication-service/src/utils/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ export enum CommunicationEvents {
22
// receive
33
JOIN = "join",
44
SEND_TEXT_MESSAGE = "send_text_message",
5+
USER_DISCONNECT = "user_disconnect",
56
DISCONNECT = "disconnect",
67

78
// send
89
USER_JOINED = "user_joined",
910
ALREADY_JOINED = "already_joined",
1011
TEXT_MESSAGE_RECEIVED = "text_message_received",
12+
USER_DISCONNECTED = "user_disconnected",
1113
DISCONNECTED = "disconnected",
1214
}
1315

1416
export enum MessageTypes {
1517
USER_GENERATED = "user_generated",
1618
BOT_GENERATED = "bot_generated",
1719
}
20+
21+
export type UserConnection = {
22+
timeout?: NodeJS.Timeout;
23+
};

frontend/src/components/Chat/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,14 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
5151
useEffect(() => {
5252
// join the room automatically when this loads
5353
communicationSocket.open();
54-
// to make sure this does not run twice
5554
communicationSocket.emit(CommunicationEvents.JOIN, {
5655
roomId: getMatchId(),
5756
username: user?.username,
5857
});
5958

6059
return () => {
61-
console.log("closing socket...");
62-
communicationSocket.close();
63-
setMessages([]); // clear the earlier messages in dev mode
60+
communicationSocket.emit(CommunicationEvents.USER_DISCONNECT);
61+
// setMessages([]); // clear the earlier messages in dev mode
6462
};
6563
// eslint-disable-next-line react-hooks/exhaustive-deps
6664
}, []);

frontend/src/contexts/CollabContext.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import { useReducer } from "react";
1616
import { updateQnHistoryById } from "../reducers/qnHistoryReducer";
1717
import qnHistoryReducer, { initialQHState } from "../reducers/qnHistoryReducer";
1818
import { CollabEvents, collabSocket, leave } from "../utils/collabSocket";
19-
import { communicationSocket } from "../utils/communicationSocket";
20-
import useAppNavigate from "../components/UseAppNavigate";
19+
import {
20+
CommunicationEvents,
21+
communicationSocket,
22+
} from "../utils/communicationSocket";
23+
import useAppNavigate from "../hooks/useAppNavigate";
2124

2225
export type CompilerResult = {
2326
status: string;
@@ -162,7 +165,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
162165
leave(partner?.id as string, getMatchId() as string, true);
163166

164167
// Leave chat room
165-
communicationSocket.disconnect();
168+
communicationSocket.emit(CommunicationEvents.USER_DISCONNECT);
166169

167170
// Delete match data
168171
stopMatch();

frontend/src/contexts/MatchContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "../utils/constants";
1414
import { useAuth } from "./AuthContext";
1515
import { toast } from "react-toastify";
16-
import useAppNavigate from "../components/UseAppNavigate";
16+
import useAppNavigate from "../hooks/useAppNavigate";
1717
import { UNSAFE_NavigationContext } from "react-router-dom";
1818
import { Action, type History, type Transition } from "history";
1919

File renamed without changes.

frontend/src/utils/debounce.ts renamed to frontend/src/hooks/useDebounce.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Dispatch, SetStateAction, useEffect, useState } from "react";
33
// Adapted from https://karthikraja555.medium.com/react-js-debouncing-or-delaying-value-change-using-custom-hook-1d5fb2e4fe79
44
const useDebounce = <T extends string | string[]>(
55
initialState: T,
6-
delay: number,
6+
delay: number
77
): [T, Dispatch<SetStateAction<T>>] => {
88
const [actualValue, setActualValue] = useState<T>(initialState);
99
const [debounceValue, setDebounceValue] = useState<T>(initialState);

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const CollabSandbox: React.FC = () => {
6767
checkPartnerStatus,
6868
isEndSessionModalOpen,
6969
resetCollab,
70+
setCompilerResult,
7071
} = collab;
7172

7273
const [state, dispatch] = useReducer(reducer, initialState);

frontend/src/pages/QuestionList/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
SUCCESS_QUESTION_DELETE,
3636
USE_AUTH_ERROR_MESSAGE,
3737
} from "../../utils/constants";
38-
import useDebounce from "../../utils/debounce";
38+
import useDebounce from "../../hooks/useDebounce";
3939
import { blue, grey } from "@mui/material/colors";
4040
import { Add, Delete, Edit, MoreVert, Search } from "@mui/icons-material";
4141
import ConfirmationDialog from "../../components/ConfirmationDialog";
@@ -121,7 +121,7 @@ const QuestionList: React.FC = () => {
121121
if (state.questionCount % rowsPerPage !== 1 || page === 0) {
122122
updateQuestionList();
123123
} else {
124-
setPage(page => page - 1);
124+
setPage((page) => page - 1);
125125
}
126126
};
127127

0 commit comments

Comments
 (0)