Skip to content

Commit

Permalink
Merge pull request #211 from boostcampwm-2024/feature/#210
Browse files Browse the repository at this point in the history
[Feat] λ°© μ œν•œ 인원 / νƒ­ μ—°κ²° λŠμ—ˆμ„ λ•Œ λ‚˜κ°€κΈ° κΈ°λŠ₯ κ΅¬ν˜„
  • Loading branch information
zizonyoungjun authored Dec 2, 2024
2 parents 67ae5cc + c29c46c commit 261717f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ import {
IRoomPayload,
} from '@/types/signaling.types';
import { getTargetSocket } from '@/util/handlerUtils';
import { getRoom } from '@/util/handlerUtils';
import { ROOM_CONSTANTS } from '@/constants/roomConstants';

const handleSignaling = (socket: ISignalingSocket, io: Server) => {
socket.on('create_room', async (payload: IRoomPayload) => {
await socket.join(payload.gsid);
socket.data.gsid = payload.gsid;
console.log('[Server][πŸŽ₯] create_room', payload.gsid);
});
socket.on('join_room', async (payload: IRoomPayload) => {
await socket.join(payload.gsid);
const roomSize = await getRoom(io, payload.gsid)?.size;
if (!roomSize || roomSize >= ROOM_CONSTANTS.maxParticipants) return;
socket.data.gsid = payload.gsid;
await socket.join(payload.gsid);
console.log('[Server][πŸŽ₯] join_room', payload.gsid);
socket
.to(payload.gsid)
.emit('user_joined', { fromUserId: socket.data.userId, gsid: payload.gsid });
Expand Down
8 changes: 6 additions & 2 deletions packages/backend/signalingServer/src/handlers/handleSocket.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Socket } from 'socket.io';
import { SOCKET_MESSAGES } from '@/constants/socketConstants';

const handleSocket = (socket: Socket) => {
socket.on('disconnect', () => {
console.log(SOCKET_MESSAGES.disconnect);
socket.to(socket.data.gsid).emit('disconnect_event', {
fromUserId: socket.data.userId,
gsid: socket.data.gsid,
});
socket.leave(socket.data.gsid);
console.log('[Server][πŸ””] disconnect_event', socket.data.userId, socket.data.gsid);
});
};

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/hooks/useJoinRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export default function useJoinRoom() {
if (!userId || !socket || !signalingSocket) return;

socket.emit('join_room', { gsid });
signalingSocket.emit('join_room', { gsid });
socket.on('join_room_success', (data) => {
setRoomData(gsid, data.isHost, false);
setAllUsers(data.userIds);
signalingSocket.emit('join_room', { gsid });
navigate(`/game/${gsid}`);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/states/store/peerConnectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const usePeerConnectionStore = create<IPeerConnectionState>((set, get) =>
peerConnection.onconnectionstatechange = () => {
const state = peerConnection.connectionState;
console.log(`[Client][πŸŽ₯] μ—°κ²° μƒνƒœ (${fromUserId}):`, state);
if (state === 'closed' || state === 'failed') {
if (state === 'closed' || state === 'failed' || state === 'disconnected') {
get().removePeerConnection(fromUserId);
get().removeRemoteStream(fromUserId);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/frontend/src/states/store/signalingSocketStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ export const useSignalingSocketStore = create<ISignalingSocketStore>((set, get)
console.log('[Client][πŸ“’] user_left', fromUserId);
});

console.log('[Client][πŸ””] setupEventHandlers');
signalingSocket.on('disconnect_event', async ({ fromUserId, gsid }) => {
const peerConnections = usePeerConnectionStore.getState().peerConnections;
const peerConnection = peerConnections.get(fromUserId)?.connection;
if (!peerConnection) return;
await peerConnection.close();
removePeerConnection(fromUserId);
removeRemoteStream(fromUserId);
console.log('[Client][πŸ“’] disconnect_event', fromUserId, gsid);
});
},

removeEventHandlers: () => {
Expand Down

0 comments on commit 261717f

Please sign in to comment.