Skip to content

Commit

Permalink
Merge pull request #319 from Game-as-a-Service/feat/socket-event-of-r…
Browse files Browse the repository at this point in the history
…oom-join-and-leave

refactor: add socket event of join room and leave room
  • Loading branch information
JohnsonMao authored Sep 17, 2023
2 parents fe9e41b + 19eaedd commit 58a31c9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/rooms/RoomChatroom/RoomChatroom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function RoomChatroom({ roomId }: RoomChatroom) {
if (!socket || !socket.connected) return;
joinChatroom(roomId);
return () => leaveChatroom(roomId);
}, [joinChatroom, leaveChatroom, roomId, socket]);
}, [joinChatroom, leaveChatroom, roomId, socket, socket.connected]);

// update message list while received new message from websocket
useEffect(() => {
Expand Down
12 changes: 6 additions & 6 deletions containers/provider/ChatroomProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,34 @@ function useChatroomCore() {
);

const joinChatroom = useCallback(
(chatroomId: string) => {
(roomId: string) => {
if (!currentUser) return;
if (socket) {
const payload = {
user: {
id: currentUser.id,
nickname: currentUser.nickname,
},
chatroomId,
target: `ROOM_${roomId}`,
};
socket?.emit(SOCKET_EVENT.CHATROOM_JOIN, payload);
socket?.emit(SOCKET_EVENT.JOIN_ROOM, payload);
}
},
[currentUser, socket]
);

const leaveChatroom = useCallback(
(chatroomId: string) => {
(roomId: string) => {
if (!currentUser) return;
if (socket) {
const payload = {
user: {
id: currentUser.id,
nickname: currentUser.nickname,
},
chatroomId,
target: `ROOM_${roomId}`,
};
socket?.emit(SOCKET_EVENT.CHATROOM_LEAVE, payload);
socket?.emit(SOCKET_EVENT.LEAVE_ROOM, payload);
}
},
[currentUser, socket]
Expand Down
4 changes: 2 additions & 2 deletions contexts/SocketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const config =
export enum SOCKET_EVENT {
CONNECT = "connect",
DISCONNECT = "disconnect",
CHATROOM_JOIN = "CHATROOM_JOIN",
CHATROOM_LEAVE = "CHATROOM_LEAVE",
JOIN_ROOM = "JOIN_ROOM",
LEAVE_ROOM = "LEAVE_ROOM",
CHAT_MESSAGE = "CHAT_MESSAGE",
USER_JOINED = "USER_JOINED",
USER_LEFT = "USER_LEFT",
Expand Down

0 comments on commit 58a31c9

Please sign in to comment.