-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
479 lines (405 loc) · 14.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import express from "express";
import http from "http";
import path from "path";
import bodyParser from 'body-parser';
import { Server } from "socket.io";
import mediasoup from 'mediasoup';
import cors from "cors";
import routes from "./routers/router.js";
import { config } from 'dotenv';
import AWS from 'aws-sdk';
config();
//변수 설정
let worker;
let workers = [];
let peers = {};
let transports = [];
let producers = [];
let consumers = [];
let gameMode;
let rooms = {};
const __dirname = path.resolve();
const app = express();
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
app.use("/", routes(rooms, gameMode));
app.post("/testdata2", (req, res) => {
const type = req.body;
gameMode = type["selectedValue"];
})
// 서버, mediasoup 설정
const httpServer = http.createServer(app);
httpServer.listen(3000, () => {
console.log("Listening on port: http://localhost:3000");
});
//cors 설정
const io = new Server(httpServer, {
cors: {
origin: "*"
}
});
// 소켓 선언
const connections = io.of("/mediasoup");
const dataConnections = io.of("/data");
// Worker 생성 함수
const createWorker = async () => {
worker = await mediasoup.createWorker({
rtcMinPort: 2000,
rtcMaxPort: 3000,
})
console.log(`worker pid ${worker.pid}`);
worker.on("died", error => {
console.error("mediasoup worker has died");
setTimeout(() => process.exit(1), 2000);
})
worker.using = false;
workers.push(worker);
return worker;
}
// Worker 생성
for (let i = 0; i < 4; i++) {
createWorker();
}
// 사용할 오디오 및 비디오 코덱 정의
const mediaCodecs = [
{
kind: "audio",
mimeType: "audio/opus",
clockRate: 48000,
channels: 2,
},
{
kind: "video",
mimeType: "video/VP8",
clockRate: 90000,
parameters: {
"x-google-start-bitrate": 300,
"max-fs": 3600,
"max-br": 500
},
},
];
// peers 객체에서 소켓을 이용한 이벤트 처리
connections.on("connection", async socket => {
console.log(socket.id);
// 소켓 연결 성공
socket.emit("connection-success", {
socketId: socket.id,
});
const removeItems = (items, socketId, type) => {
items.forEach(item => {
if (item.socketId === socket.id) {
item[type].close();
}
})
items = items.filter(item => item.socketId !== socket.id);
return items;
}
// 소켓 연결 종료
socket.on("disconnect", () => {
console.log("peer disconnected");
consumers = removeItems(consumers, socket.id, "consumer");
producers = removeItems(producers, socket.id, "producer");
transports = removeItems(transports, socket.id, "transport");
const peer = peers[socket.id];
if (!peer) {
return;
}
const { roomName } = peer;
if (roomName && rooms[roomName]) {
rooms[roomName].peers = rooms[roomName].peers.filter((socketId) => socketId !== socket.id);
// 방에 접속한 인원이 1명만 남아 있는 경우
if (rooms[roomName].peers.length === 0) {
// 방 이름과 같은 worker의 router를 삭제
for (const work of workers) {
let workList = Array.from(work.getRouters());
let router = workList.find((temp) => temp.room === roomName);
if (router) {
router.close();
console.log("삭제 후 router 개수:", work.getRouters().size);
break;
}
}
delete rooms[roomName]; // 방 삭제
}
}
delete peers[socket.id];
console.log("종료 후 방 목록:", rooms);
});
// 방에 입장시 joinRoom 이벤트 수신
socket.on("joinRoom", async ({ roomName }, callback) => {
const router1 = await createRoom(roomName, socket.id);
peers[socket.id] = {
socket,
roomName,
transports: [],
producers: [],
consumers: [],
peerDetails: {
name: '',
isAdmin: false,
}
}
console.log();
console.log(rooms);
for (const room in rooms) {
const name = room;
let roomLen = rooms[room].peers.length;
console.log(`방 이름 : ${name}, 참여 인원 : ${roomLen} 명`);
}
const rtpCapabilities = router1.rtpCapabilities;
workers.forEach(worker => {
console.log(`${worker.pid}에 연결된 routers:`, worker.getRouters());
})
callback({ rtpCapabilities });
});
// transport 생성 이벤트 수신
socket.on("createWebRtcTransport", async ({ consumer }, callback) => {
const roomName = peers[socket.id].roomName;
const router = rooms[roomName].router;
createWebRtcTransport(router).then(
transport => {
callback({
params: {
id: transport.id,
iceParameters: transport.iceParameters,
iceCandidates: transport.iceCandidates,
dtlsParameters: transport.dtlsParameters,
}
})
addTransport(transport, roomName, consumer);
},
error => {
console.log(error);
});
})
// 공급자 정보 받기
socket.on("getProducers", callback => {
const { roomName } = peers[socket.id];
let producerList = [];
producers.forEach(producerData => {
if (producerData.socketId !== socket.id && producerData.roomName === roomName) {
producerList = [...producerList, producerData.producer.id];
}
})
callback(producerList);
})
// 만들어진 transport 연결
socket.on("transport-connect", ({ dtlsParameters }) => {
console.log("DTLS PARAMS...", { dtlsParameters });
getTransport(socket.id).connect({ dtlsParameters });
})
// 공급 속성의 transport 생성
socket.on("transport-produce", async ({ kind, rtpParameters, appData }, callback) => {
const producer = await getTransport(socket.id).produce({
kind,
rtpParameters,
})
if (!peers[socket.id]) {
return;
}
const { roomName } = peers[socket.id];
addProducer(producer, roomName);
informConsumers(roomName, socket.id, producer.id);
console.log("Producer ID: ", producer.id, producer.kind)
producer.on("transportclose", () => {
console.log("transport for this producer closed");
producer.close();
})
callback({
id: producer.id,
producersExist: producers.length > 1 ? true : false
})
})
// transport 수신 성공
socket.on("transport-recv-connect", async ({ dtlsParameters, serverConsumerTransportId }) => {
console.log(`DTLS PARAMS: ${dtlsParameters}`);
const consumerTransport = transports.find(transportData => (
transportData.consumer && transportData.transport.id == serverConsumerTransportId
)).transport
await consumerTransport.connect({ dtlsParameters });
});
// transport consume 성공
socket.on("consume", async ({ rtpCapabilities, remoteProducerId, serverConsumerTransportId }, callback) => {
try {
const { roomName } = peers[socket.id];
const router = rooms[roomName].router;
let consumerTransport = transports.find(transportData => (
transportData.consumer && transportData.transport.id == serverConsumerTransportId
)).transport
if (router.canConsume({
producerId: remoteProducerId,
rtpCapabilities
})) {
const consumer = await consumerTransport.consume({
producerId: remoteProducerId,
rtpCapabilities,
paused: true,
})
consumer.on("transportclose", () => {
console.log("transport close from consumer");
})
consumer.on("producerclose", () => {
console.log("producer of consumer closed");
socket.emit("producer-closed", { remoteProducerId });
consumerTransport.close([]);
transports = transports.filter(transportData => transportData.transport.id !== consumerTransport.id);
consumer.close();
consumers = consumers.filter(consumerData => consumerData.consumer.id !== consumer.id);
});
addConsumer(consumer, roomName);
const params = {
id: consumer.id,
producerId: remoteProducerId,
kind: consumer.kind,
rtpParameters: consumer.rtpParameters,
serverConsumerId: consumer.id,
}
callback({ params });
}
} catch (error) {
console.log(error.message);
callback({
params: {
error: error
}
})
}
})
// transport consume 재개
socket.on("consumer-resume", async ({ serverConsumerId }) => {
console.log("consumer resume");
const { consumer } = consumers.find(consumerData => consumerData.consumer.id === serverConsumerId);
await consumer.resume();
});
// 함수 선언
const createRoom = async (roomName, socketId) => {
let router1;
let peers = [];
if (rooms[roomName]) {
router1 = rooms[roomName].router;
peers = rooms[roomName].peers || [];
rooms[roomName] = {
router: router1,
peers: [...peers, socketId],
roomType: rooms[roomName].roomType
}
} else {
for (const worker of workers) {
if (worker.getRouters().size < 4) {
worker.using = roomName;
router1 = await worker.createRouter({ mediaCodecs, });
router1["room"] = roomName;
break;
}
}
rooms[roomName] = {
router: router1,
peers: [...peers, socketId],
roomType: gameMode
}
}
console.log(`Router ID: ${router1.id}`);
return router1;
}
const addTransport = (transport, roomName, consumer) => {
transports = [
...transports,
{ socketId: socket.id, transport, roomName, consumer, }
]
peers[socket.id] = {
...peers[socket.id],
transports: [
...peers[socket.id].transports,
transport.id,
]
}
}
const addProducer = (producer, roomName) => {
producers = [
...producers,
{ socketId: socket.id, producer, roomName, }
]
peers[socket.id] = {
...peers[socket.id],
producers: [
...peers[socket.id].producers,
producer.id,
]
}
}
const addConsumer = (consumer, roomName) => {
consumers = [
...consumers,
{ socketId: socket.id, consumer, roomName, }
]
peers[socket.id] = {
...peers[socket.id],
consumers: [
...peers[socket.id].consumers,
consumer.id,
]
}
}
const getTransport = (socketId) => {
const [producerTransport] = transports.filter(transport => transport.socketId === socketId && !transport.consumer);
return producerTransport.transport;
}
const informConsumers = (roomName, socketId, id) => {
console.log(`just joined, id ${id} ${roomName}, ${socketId}`);
producers.forEach(producerData => {
if (producerData.socketId !== socketId && producerData.roomName === roomName) {
const producerSocket = peers[producerData.socketId].socket;
producerSocket.emit("new-producer", { producerId: id });
}
})
}
const createWebRtcTransport = async (router) => {
return new Promise(async (resolve, reject) => {
try {
const webRtcTransport_options = {
listenIps: [
{
ip: '172.31.5.109', // replace with relevant IP address
announcedIp: '43.201.47.117',
}
],
enableUdp: true,
enableTcp: true,
preferUdp: true,
}
// router 내장함수 createWebRtpTransport 함수를 실행
let transport = await router.createWebRtcTransport(webRtcTransport_options);
console.log(`transport id: ${transport.id}`);
// transport 객체에 이벤트 핸들러 부착
transport.on("dtlsstatechange", dtlsState => {
if (dtlsState === "closed") {
transport.close();
}
});
transport.on("close", () => {
console.log("transport closed");
});
resolve(transport);
} catch (error) {
reject(error);
}
})
}
});
// data 소켓
dataConnections.on("connect", async socket => {
console.log("data 소켓 연결");
socket.emit("connection-success", {
socketId: socket.id,
});
socket.on("get_game_data", (data) => {
socket.broadcast.emit("send_game_data", {data },() => {
console.log("success: send data")
})
})
socket.on("disconnect", () => {
console.log("data 소켓 연결 종료");
});
})