-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: JS 접근 제어자로 변경 * refactor: WebRTC 코드 리팩터링 및 렌더링 문제 일부 해결 * docs: README.md 업데이트
- Loading branch information
1 parent
a86c705
commit 59832d9
Showing
7 changed files
with
351 additions
and
211 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { gameSocket } from '@/services/gameSocket'; | ||
import { getRoomsQuery } from '@/stores/queries/getRoomsQuery'; | ||
import useRoomStore from '@/stores/zustand/useRoomStore'; | ||
import { useCallback, useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
export const useGameRoom = (roomId: string | undefined) => { | ||
const navigate = useNavigate(); | ||
const setCurrentRoom = useRoomStore((state) => state.setCurrentRoom); | ||
const { data: rooms, isLoading } = getRoomsQuery(); | ||
|
||
// 초기 설정 및 소켓 연결 관리 | ||
useEffect(() => { | ||
// 로딩 중이면 리턴 | ||
if (isLoading) return; | ||
|
||
// roomId나 rooms가 없으면 홈으로 | ||
if (!roomId || !rooms) { | ||
navigate('/', { replace: true }); | ||
return; | ||
} | ||
|
||
const room = rooms.find((r) => r.roomId === roomId); | ||
if (!room) { | ||
navigate('/', { replace: true }); | ||
return; | ||
} | ||
|
||
// 소켓 연결 및 초기 설정 | ||
const initializeRoom = async () => { | ||
try { | ||
// 1. 소켓 연결 | ||
if (!gameSocket.socket?.connected) { | ||
gameSocket.connect(); | ||
} | ||
|
||
// 2. 방 설정 | ||
setCurrentRoom(room); | ||
|
||
// 3. 소켓 재연결 (새로고침 대응) | ||
await gameSocket.joinRoom( | ||
roomId, | ||
room.players[room.players.length - 1] | ||
); | ||
} catch (error) { | ||
console.error('Failed to initialize room:', error); | ||
navigate('/', { replace: true }); | ||
} | ||
}; | ||
|
||
initializeRoom(); | ||
|
||
// Cleanup | ||
return () => { | ||
if (gameSocket.socket?.connected) { | ||
gameSocket.disconnect(); | ||
} | ||
setCurrentRoom(null); | ||
}; | ||
}, [roomId, rooms, isLoading]); | ||
|
||
// 방 나가기 | ||
const leaveRoom = useCallback(() => { | ||
if (gameSocket.socket?.connected) { | ||
gameSocket.disconnect(); | ||
} | ||
setCurrentRoom(null); | ||
navigate('/', { replace: true }); | ||
}, [navigate, setCurrentRoom]); | ||
|
||
return { | ||
leaveRoom, | ||
}; | ||
}; |
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,83 @@ | ||
import { useCallback } from 'react'; | ||
import { gameSocket } from '@/services/gameSocket'; | ||
import { signalingSocket } from '@/services/signalingSocket'; | ||
|
||
export const useRoomActions = () => { | ||
const createRoom = useCallback( | ||
async (roomName: string, hostNickname: string) => { | ||
try { | ||
// 1. 게임 소켓 연결 | ||
gameSocket.connect(); | ||
|
||
// 2. 오디오 스트림 설정 및 방 생성 | ||
const stream = await gameSocket.createRoom(roomName, hostNickname); | ||
|
||
// 3. 시그널링 소켓 설정 | ||
signalingSocket.connect(); | ||
signalingSocket.setLocalStream(stream); | ||
|
||
// 4. 방 생성 성공 시 시그널링 룸 참여 | ||
const onRoomCreated = (room) => { | ||
signalingSocket.joinSignalingRoom(room.roomId, hostNickname); | ||
gameSocket.socket?.off('roomCreated', onRoomCreated); | ||
}; | ||
|
||
gameSocket.socket?.on('roomCreated', onRoomCreated); | ||
} catch (error) { | ||
console.error('방 생성 실패:', error); | ||
throw error; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const joinRoom = useCallback( | ||
async (roomId: string, playerNickname: string) => { | ||
try { | ||
// 1. 게임 소켓 연결 | ||
gameSocket.connect(); | ||
|
||
// 2. 오디오 스트림 설정 및 방 참여 | ||
const stream = await gameSocket.joinRoom(roomId, playerNickname); | ||
|
||
// 3. 시그널링 소켓 설정 | ||
signalingSocket.connect(); | ||
signalingSocket.setLocalStream(stream); | ||
|
||
// 4. 방 참여 성공 시 시그널링 룸 참여 | ||
const onUpdateUsers = (players) => { | ||
if (players.includes(playerNickname)) { | ||
signalingSocket.joinSignalingRoom(roomId, playerNickname); | ||
gameSocket.socket?.off('updateUsers', onUpdateUsers); | ||
} | ||
}; | ||
|
||
gameSocket.socket?.on('updateUsers', onUpdateUsers); | ||
} catch (error) { | ||
console.error('방 참여 실패:', error); | ||
throw error; | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const leaveRoom = useCallback(() => { | ||
signalingSocket.disconnect(); | ||
gameSocket.disconnect(); | ||
}, []); | ||
|
||
const toggleAudio = useCallback((enabled: boolean) => { | ||
if (gameSocket['#audioStream']) { | ||
gameSocket['#audioStream'].getAudioTracks().forEach((track) => { | ||
track.enabled = enabled; | ||
}); | ||
} | ||
}, []); | ||
|
||
return { | ||
createRoom, | ||
joinRoom, | ||
leaveRoom, | ||
toggleAudio, | ||
}; | ||
}; |
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
Oops, something went wrong.