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

[FIX]: 인게임 구독 중첩 안되도록 수정 #315

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions src/pages/GamePage/hooks/useWebsocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './init.ts';
import { Client } from '@stomp/stompjs';
import { Client, StompSubscription } from '@stomp/stompjs';
import { useEffect, useRef } from 'react';
import SockJS from 'sockjs-client';
import { BASE_PATH } from '@/generated/base';
Expand All @@ -11,6 +11,7 @@ import { PayloadType } from '../types/websocketType.ts';

const useWebsocket = (roomId: number | null) => {
const stompClient = useRef<Client>();
const ingameSubscription = useRef<StompSubscription>();

const connectHeaders = getToken();

Expand Down Expand Up @@ -117,12 +118,18 @@ const useWebsocket = (roomId: number | null) => {
};

const onIngameConnected = () => {
stompClient.current?.subscribe(
ingameSubscription.current = stompClient.current?.subscribe(
`/from/game/${roomId}`,
(e) => onIngameMessageReceived(e),
connectHeaders
);
};
const onIngameDisconnected = () => {
if (ingameSubscription.current) {
ingameSubscription.current.unsubscribe();
}
};

dlsxjzld marked this conversation as resolved.
Show resolved Hide resolved
const handleConnectIngame = (roomId: number) => {
stompClient.current?.publish({
destination: `/to/game/${roomId}/connect`,
Expand All @@ -138,6 +145,7 @@ const useWebsocket = (roomId: number | null) => {
setIsIngameWsError(true);
}
if (responsePublish.type === 'FINISH') {
onIngameDisconnected();
setAllMembers(responsePublish.allMembers);
}
};
Expand Down