-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[♻️refactor]: useWebsocket useIngameWebsocket 분리
- Loading branch information
Showing
4 changed files
with
102 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Client, StompSubscription } from '@stomp/stompjs'; | ||
import { MutableRefObject } from 'react'; | ||
import useGameWaitingRoomStore from '@/store/useGameWaitingRoomStore'; | ||
import useIngameStore from '@/store/useIngameStore'; | ||
import { checkIsEmptyObj } from '@/utils/checkIsEmptyObj'; | ||
import { getToken } from '@/utils/getToken'; | ||
import { PayloadType } from '../types/websocketType'; | ||
|
||
interface I_useIngame { | ||
roomId: number; | ||
stompClient: MutableRefObject<Client | undefined>; | ||
ingameSubscription: MutableRefObject<StompSubscription | undefined>; | ||
} | ||
const useIngameWebsocket = ({ | ||
roomId, | ||
stompClient, | ||
ingameSubscription, | ||
}: I_useIngame) => { | ||
const connectHeaders = getToken(); | ||
const { setIsIngameWsError, setIngameRoomRes } = useIngameStore(); | ||
const { setAllMembers } = useGameWaitingRoomStore(); | ||
|
||
const onIngameConnected = () => { | ||
ingameSubscription.current = stompClient.current?.subscribe( | ||
`/from/game/${roomId}`, | ||
(e) => onIngameMessageReceived(e), | ||
connectHeaders | ||
); | ||
}; | ||
const disconnectIngameWs = () => { | ||
ingameSubscription.current?.unsubscribe(); | ||
}; | ||
|
||
const handleConnectIngame = (roomId: number) => { | ||
stompClient.current?.publish({ | ||
destination: `/to/game/${roomId}/connect`, | ||
headers: connectHeaders, | ||
}); | ||
}; | ||
const onIngameMessageReceived = ({ body }: { body: string }) => { | ||
const responsePublish = JSON.parse(body); | ||
// eslint-disable-next-line no-console | ||
console.log('ingameResponsePublish-----', responsePublish); | ||
setIngameRoomRes(responsePublish); | ||
if (checkIsEmptyObj(responsePublish)) { | ||
setIsIngameWsError(true); | ||
} | ||
if (responsePublish.type === 'FINISH') { | ||
disconnectIngameWs(); | ||
setAllMembers(responsePublish.allMembers); | ||
} | ||
}; | ||
const publishIngame = (destination: string, payload: PayloadType) => { | ||
stompClient.current?.publish({ | ||
destination: `/to/game/${roomId}${destination}`, | ||
headers: connectHeaders, | ||
body: JSON.stringify(payload), | ||
}); | ||
}; | ||
|
||
return { | ||
onIngameConnected, | ||
handleConnectIngame, | ||
publishIngame, | ||
}; | ||
}; | ||
|
||
export default useIngameWebsocket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters