Skip to content

Commit

Permalink
[πŸ›fix]: μΈκ²Œμž„ ꡬ독 쀑첩 μ•ˆλ˜λ„λ‘ μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
dlsxjzld committed Apr 4, 2024
1 parent ced5666 commit b4c42b5
Showing 1 changed file with 10 additions and 2 deletions.
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();
}
};

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

0 comments on commit b4c42b5

Please sign in to comment.