Skip to content

Commit

Permalink
feat: add user leave api when socket disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
Turtle-Hwan committed Aug 19, 2024
1 parent 6a120c9 commit d40eac6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/apis/Chat/Socket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Stomp } from "@stomp/stompjs";
import SockJS from "sockjs-client";

import { createRequestOptionsJSON_AUTH, fetchApi } from "@/apis/_createRequestOptions";

export const SocketConnect = (
stompClient: any,
chatRoomId: string,
Expand All @@ -13,6 +15,7 @@ export const SocketConnect = (
if (!token || !stompClient.current.onConnect) {
//๋กœ๊ทธ์ธ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ
//alert("๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.");
console.log("!token || !stompClient.current.onConnect, ๋กœ๊ทธ์ธ ์˜ค๋ฅ˜");
return;
}

Expand All @@ -37,3 +40,14 @@ export const SocketDisconnect = (stompClient: any) => {
stompClient.current.disconnect();
}
};

/** */
export const ChatroomLeave = async (chatRoomId: number) => {
const requestOptions = createRequestOptionsJSON_AUTH("POST");
const spaceId = Number(localStorage.getItem("spaceId"));
if (!requestOptions || !spaceId) return null;

const url = `${import.meta.env.VITE_API_BACK_URL}/space/${spaceId}/chat/${chatRoomId}/leave`;

return await fetchApi(url, requestOptions);
};
2 changes: 2 additions & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export * from "@/apis/Space/SpaceSearchAllUserApi";
export * from "@/apis/Space/SpaceSearchUserProfile";
export * from "@/apis/Space/SpaceSelectApi";
export * from "@/apis/Space/SpaceUserJoinApi";

export * from "@/apis/_createRequestOptions";
8 changes: 6 additions & 2 deletions src/pages/ChatPage/ChattingPage/ChattingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ChatPay,
ChatPost,
Chatroom,
ChatroomLeave,
ChatSendRequestFrame,
ChatText,
SocketConnect,
Expand Down Expand Up @@ -78,8 +79,11 @@ const ChattingPage = () => {
});

SocketConnect(stompClient, param.id || "", handleChatMessage);
return () => SocketDisconnect(stompClient);
}, [param.id, spaceId]);
return () => {
SocketDisconnect(stompClient);
ChatroomLeave(chatroomInfo.id).then((res) => console.log(res)); //์‚ฌ์šฉ์ž ์ฑ„ํŒ…๋ฐฉ ๋– ๋‚จ ์•Œ๋ ค์ฃผ๊ธฐ (unsubscribe..?)
};
}, [param.id, spaceId, chatroomInfo.id]);

//
//
Expand Down

0 comments on commit d40eac6

Please sign in to comment.