|
| 1 | +import { |
| 2 | + WebSocketGateway, |
| 3 | + WebSocketServer, |
| 4 | + OnGatewayConnection, |
| 5 | + OnGatewayDisconnect, |
| 6 | + SubscribeMessage, |
| 7 | + MessageBody, |
| 8 | +} from "@nestjs/websockets"; |
| 9 | +import { Server } from "socket.io"; |
| 10 | + |
| 11 | +interface User { |
| 12 | + id: string; |
| 13 | + nickname: string; |
| 14 | +} |
| 15 | + |
| 16 | +@WebSocketGateway({ |
| 17 | + cors: { |
| 18 | + origin: "*", // CORS 설정 |
| 19 | + }, |
| 20 | +}) |
| 21 | +export class SocketGateway implements OnGatewayConnection, OnGatewayDisconnect { |
| 22 | + @WebSocketServer() |
| 23 | + server: Server; |
| 24 | + |
| 25 | + private users: { [key: string]: User[] } = {}; |
| 26 | + private socketToRoom: { [key: string]: string } = {}; |
| 27 | + private maximum = 5; |
| 28 | + |
| 29 | + handleConnection(socket: any) { |
| 30 | + console.log(`Client connected: ${socket.id}`); |
| 31 | + } |
| 32 | + |
| 33 | + handleDisconnect(socket: any) { |
| 34 | + console.log(`Client disconnected: ${socket.id}`); |
| 35 | + const roomID = this.socketToRoom[socket.id]; |
| 36 | + if (roomID) { |
| 37 | + const room = this.users[roomID]; |
| 38 | + if (room) { |
| 39 | + this.users[roomID] = room.filter( |
| 40 | + (user) => user.id !== socket.id |
| 41 | + ); |
| 42 | + if (this.users[roomID].length === 0) { |
| 43 | + delete this.users[roomID]; |
| 44 | + } else { |
| 45 | + this.server.to(roomID).emit("user_exit", { id: socket.id }); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @SubscribeMessage("join_room") |
| 52 | + handleJoinRoom(socket: any, data: { room: string; nickname: string }) { |
| 53 | + if (this.users[data.room]) { |
| 54 | + if (this.users[data.room].length === this.maximum) { |
| 55 | + socket.emit("room_full"); |
| 56 | + return; |
| 57 | + } |
| 58 | + this.users[data.room].push({ |
| 59 | + id: socket.id, |
| 60 | + nickname: data.nickname, |
| 61 | + }); |
| 62 | + } else { |
| 63 | + this.users[data.room] = [ |
| 64 | + { id: socket.id, nickname: data.nickname }, |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + this.socketToRoom[socket.id] = data.room; |
| 69 | + socket.join(data.room); |
| 70 | + console.log(`[${data.room}]: ${socket.id} enter`); |
| 71 | + |
| 72 | + const usersInThisRoom = this.users[data.room].filter( |
| 73 | + (user) => user.id !== socket.id |
| 74 | + ); |
| 75 | + socket.emit("all_users", usersInThisRoom); |
| 76 | + } |
| 77 | + |
| 78 | + @SubscribeMessage("offer") |
| 79 | + handleOffer( |
| 80 | + @MessageBody() |
| 81 | + data: { |
| 82 | + offerReceiveID: string; |
| 83 | + sdp: any; |
| 84 | + offerSendID: string; |
| 85 | + offerSendNickname: string; |
| 86 | + } |
| 87 | + ) { |
| 88 | + this.server.to(data.offerReceiveID).emit("getOffer", { |
| 89 | + sdp: data.sdp, |
| 90 | + offerSendID: data.offerSendID, |
| 91 | + offerSendNickname: data.offerSendNickname, |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @SubscribeMessage("answer") |
| 96 | + handleAnswer( |
| 97 | + @MessageBody() |
| 98 | + data: { |
| 99 | + answerReceiveID: string; |
| 100 | + sdp: any; |
| 101 | + answerSendID: string; |
| 102 | + } |
| 103 | + ) { |
| 104 | + this.server.to(data.answerReceiveID).emit("getAnswer", { |
| 105 | + sdp: data.sdp, |
| 106 | + answerSendID: data.answerSendID, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + @SubscribeMessage("candidate") |
| 111 | + handleCandidate( |
| 112 | + @MessageBody() |
| 113 | + data: { |
| 114 | + candidateReceiveID: string; |
| 115 | + candidate: any; |
| 116 | + candidateSendID: string; |
| 117 | + } |
| 118 | + ) { |
| 119 | + this.server.to(data.candidateReceiveID).emit("getCandidate", { |
| 120 | + candidate: data.candidate, |
| 121 | + candidateSendID: data.candidateSendID, |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments