-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 게임방 입장(joinRoom) Socket 이벤트 함수 구현 * chore: 변수명 변경 * feat: 사용자 게임방 입장 시 상태 변경 * chore: Socket ID console.log에서 제거
- Loading branch information
1 parent
048a8e9
commit aabdeca
Showing
6 changed files
with
183 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,113 @@ | ||
import { JoinGameRoomResult, Room } from '@/types/roomTypes'; | ||
import { | ||
ClientToServerEvents, | ||
ServerToClientEvents, | ||
} from '@/types/socketTypes'; | ||
import { io, Socket } from 'socket.io-client'; | ||
|
||
const SOCKET_BASE_URL = 'wss://game.clovapatra.com'; | ||
|
||
const gameSocket: Socket<ServerToClientEvents, ClientToServerEvents> = io( | ||
`${SOCKET_BASE_URL}/rooms`, | ||
{ | ||
transports: ['websocket'], | ||
withCredentials: true, | ||
} | ||
); | ||
|
||
// 소켓 연결 상태 모니터링 | ||
gameSocket.on('connect', () => { | ||
console.log('Socket connected'); | ||
}); | ||
|
||
gameSocket.on('connect_error', (error) => { | ||
console.error('Socket connection error:', error); | ||
}); | ||
|
||
gameSocket.on('disconnect', (reason) => { | ||
console.log('Socket disconnected:', reason); | ||
}); | ||
|
||
export const createRoom = async ( | ||
roomName: string, | ||
hostNickname: string | ||
): Promise<Room> => { | ||
return new Promise((resolve, reject) => { | ||
gameSocket.emit('createRoom', { roomName, hostNickname }); | ||
|
||
gameSocket.on('roomCreated', (room) => { | ||
resolve(room); | ||
}); | ||
|
||
gameSocket.on('error', (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
|
||
export const joinRoom = async ( | ||
roomId: string, | ||
playerNickname: string | ||
): Promise<JoinGameRoomResult> => { | ||
if (!gameSocket.connected) { | ||
throw new Error('서버와 연결이 되지 않았습니다.'); | ||
} | ||
|
||
console.log('Attempting to join room:', { roomId, playerNickname }); | ||
|
||
return new Promise((resolve, reject) => { | ||
let isResolved = false; | ||
|
||
// updateUsers 이벤트 핸들러 | ||
const handleUpdateUsers = (players: string[]) => { | ||
console.log('Received updateUsers:', players); | ||
|
||
// 첫 업데이트에서만 resolve 하도록 | ||
if (!isResolved) { | ||
isResolved = true; | ||
cleanup(); | ||
|
||
// Room 객체 구성 | ||
const room: Room = { | ||
roomId, | ||
roomName: `Room ${roomId}`, // 서버에서 따로 제공하지 않음 | ||
hostNickname: players[0], // 첫 번째 플레이어를 호스트로 가정 | ||
players: players, | ||
status: 'waiting', | ||
}; | ||
|
||
resolve({ | ||
room, | ||
stream: new MediaStream(), | ||
}); | ||
} | ||
}; | ||
|
||
const handleError = (error: { code: string; message: string }) => { | ||
console.error('Error joining room:', error); | ||
cleanup(); | ||
reject(error); | ||
}; | ||
|
||
const cleanup = () => { | ||
gameSocket.off('updateUsers', handleUpdateUsers); | ||
gameSocket.off('error', handleError); | ||
clearTimeout(timeoutId); | ||
}; | ||
|
||
// 이벤트 리스너 등록 | ||
gameSocket.on('updateUsers', handleUpdateUsers); | ||
gameSocket.on('error', handleError); | ||
|
||
// 방 입장 요청 | ||
gameSocket.emit('joinRoom', { roomId, playerNickname }); | ||
|
||
// 타임아웃 설정 | ||
const timeoutId = setTimeout(() => { | ||
if (!isResolved) { | ||
cleanup(); | ||
reject(new Error('서버 응답 시간이 초과되었습니다.')); | ||
} | ||
}, 5000); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,38 @@ | ||
import { Room } from './roomTypes'; | ||
|
||
// 게임 서버 이벤트 타입 | ||
export interface ServerToClientEvents { | ||
roomCreated: (room: Room) => void; | ||
roomJoined: (room: Room) => void; | ||
roomLeft: (room: Room) => void; | ||
updateUsers: (players: string[]) => void; | ||
error: (error: { code: string; message: string }) => void; | ||
} | ||
|
||
export interface ClientToServerEvents { | ||
createRoom: (data: { roomName: string; hostNickname: string }) => void; | ||
joinRoom: (data: { roomId: string; userNickname: string }) => void; | ||
joinRoom: (data: { roomId: string; playerNickname: string }) => void; | ||
leaveRoom: (data: { roomId: string }) => void; | ||
} | ||
|
||
// 시그널링 서버 이벤트 타입 | ||
export interface SignalingServerToClientEvents { | ||
joinSuccess: () => void; | ||
'peer-joined': (data: { peerId: string; userId: string }) => void; | ||
'peer-left': (data: { peerId: string }) => void; | ||
offer: (data: { targetId: string; sdp: RTCSessionDescriptionInit }) => void; | ||
answer: (data: { targetId: string; sdp: RTCSessionDescriptionInit }) => void; | ||
'ice-candidate': (data: { | ||
targetId: string; | ||
candidate: RTCIceCandidateInit; | ||
}) => void; | ||
} | ||
|
||
export interface SignalingClientToServerEvents { | ||
join: (data: { roomId: string; userId: string }) => void; | ||
leave: (data: { roomId: string }) => void; | ||
offer: (data: { targetId: string; sdp: RTCSessionDescriptionInit }) => void; | ||
answer: (data: { targetId: string; sdp: RTCSessionDescriptionInit }) => void; | ||
'ice-candidate': (data: { | ||
targetId: string; | ||
candidate: RTCIceCandidateInit; | ||
}) => void; | ||
} |