-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (70 loc) · 2.35 KB
/
app.js
File metadata and controls
83 lines (70 loc) · 2.35 KB
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
const WebSocket = require('ws');
const { v4: uuidv4 } = require('uuid');
const wss = new WebSocket.Server({port:5000});
const sessions = new Map();
const rooms = {};
// 서버 시작 알림
wss.on('listening', () => {
console.log('WebSocket server started on port 5000');
});
// 메시지 처리
wss.on('connection', (ws, req) => {
const sessionId = uuidv4();
sessions.set(sessionId, ws);
ws.send(`Welcome! Your session ID is: ${sessionId}`);
// 메시지 수신 이벤트 처리
ws.on('message', (message) => {
// 받을 json
/*{
"trackId": "3f9e01c8-dcd5-4b91-9dbf-ace73fb82032",
"time": "2024-10-05T12:34:56Z" or null
}*/
try{
const receivedData = JSON.parse(message);
const trackId = receivedData.trackId;
const time = receivedData.time;
// 방에 JOIN 하는 로직
if(time == null){
// 새로운 방(track) 생성
if(!rooms[trackId]) {
rooms[trackId] = [];
}
rooms[trackId].push(sessionId);
console.log(rooms[trackId]);
ws.send("you joined " + trackId);
} else /*전체에게 메시지 전송하는 로직*/ {
rooms[trackId].map((id) => {
const clientWs = sessions.get(id);
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
clientWs.send(time);
}
return null;
});
}
} catch (error){
ws.send(JSON.stringify({
error: error.message // 에러 메시지 전송
}));
}
});
// 연결 종료 시 처리
ws.on('close', () => {
console.log(`Client with session ID ${sessionId} disconnected`);
sessions.delete(sessionId); // 세션 삭제
// 방에서 세션 ID 삭제
for (const trackId in rooms) {
if (rooms[trackId].includes(sessionId)) {
rooms[trackId] = rooms[trackId].filter(id => id !== sessionId); // 세션 ID 제거
console.log(`Session ${sessionId} removed from room ${trackId}`);
// 방이 비어 있으면 방 삭제
if (rooms[trackId].length === 0) {
delete rooms[trackId];
console.log(`Room ${trackId} is now empty and has been deleted.`);
} else {
console.log(`Current users in room ${trackId}:`, rooms[trackId]);
}
break; // 이미 세션 ID를 찾아서 제거했으므로 루프 종료
}
}
});
});