Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 소켓 연결 해제 개선 #68

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Components/Chat/ChatHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { getCookie } from '@/Components/Login/Cookie';
import { useRouter } from 'next/navigation';
import { Socket } from 'socket.io-client';
import { DefaultEventsMap } from 'socket.io/dist/typed-events';

const ChatHeader = ({
socket,
chatId,
chatName,
chatUsers,
}: {
socket: Socket<DefaultEventsMap, DefaultEventsMap>;
chatId: string;
chatName: string;
chatUsers: number;
Expand All @@ -19,7 +15,6 @@ const ChatHeader = ({
const accessToken = getCookie('accessToken');

const handleBackChat = () => {
socket.disconnect();
router.back();
};

Expand All @@ -34,7 +29,6 @@ const ChatHeader = ({
body: JSON.stringify({ chatId }),
});

socket.disconnect();
router.back();
};

Expand Down
11 changes: 0 additions & 11 deletions Components/Chat/ChatRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ const ChatRoom = ({
});

socket.on('join', (responseData) => {
// console.log('join 이벤트 리슨');
// console.log('responseData: ', responseData);
// console.log('responseData.users: ', responseData.users);
// console.log('responseData.joiner: ', responseData.joiners);

setChatUsers([...chatUsers, ...responseData.users]);
socket.emit(
'message-to-server',
Expand All @@ -70,11 +65,6 @@ const ChatRoom = ({
});

socket.on('leave', (responseData) => {
// console.log('leave 이벤트 리슨');
// console.log('responseData: ', responseData);
// console.log('responseData.users: ', responseData.users);
// console.log('responseData.leaver: ', responseData.leaver);

setChatUsers([...chatUsers, ...responseData.users]);
socket.emit(
'message-to-server',
Expand Down Expand Up @@ -115,7 +105,6 @@ const ChatRoom = ({
return (
<>
<ChatHeader
socket={socket}
chatId={chatId}
chatName={chatName}
chatUsers={chatUsers.length}
Expand Down
8 changes: 8 additions & 0 deletions app/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSearchParams } from 'next/navigation';
import { getCookie } from '@/Components/Login/Cookie';
import { io } from 'socket.io-client';
import ChatRoom from '@/Components/Chat/ChatRoom';
import { useEffect } from 'react';

const Chat = ({ params }: { params: { id: string } }) => {
const chatId = params.id;
Expand All @@ -18,6 +19,13 @@ const Chat = ({ params }: { params: { id: string } }) => {
},
});

useEffect(() => {
return () => {
socket.disconnect();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<ChatRoom
socket={socket}
Expand Down
Loading