-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
182 lines (171 loc) · 7.26 KB
/
server.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
import app from './src/app.js';
import config from './src/config/config.js';
import { connectDB, connectRedis } from './src/config/db.js';
import { server } from './src/app.js';
import { createSession } from './src/services/openviduService.js';
import { io } from './src/app.js';
import { findBestMatch } from './src/services/matchingService.js';
const MATCH_MAKING_PERSON_NUMBERS = 4;
const BEST_MATCHING_PERSON_NUMBERS = 3;
const userSocketMap = new Map();
const redisClient = await connectRedis.connectRedis();
const redisSub = await connectRedis.connectRedis();
let lastQueueLength = 0;
let throttleTimeout = null;
redisSub.subscribe('matchmaking');
redisSub.on('message', async (channel, message) => {
if (channel === 'matchmaking' && message === 'match') {
const users = await redisClient.hvals('waiting_queue');
const parsedUsers = users.map((user) => JSON.parse(user));
console.log('parsed', parsedUsers);
const matchedGroups = [];
while (parsedUsers.length >= MATCH_MAKING_PERSON_NUMBERS) {
// 5명 이상일 때만 그룹 매칭 시작
const user = parsedUsers.shift(); //기준이 될 유저(맨 처음 사용자)
// console.log('기준:', user);
const bestMatches = await findBestMatch(user, parsedUsers);
if (bestMatches.length === BEST_MATCHING_PERSON_NUMBERS) {
matchedGroups.push([user, ...bestMatches]);
for (const matchedUser of bestMatches) {
const index = parsedUsers.findIndex(
(u) => u.userId === matchedUser.userId
);
if (index > -1) {
parsedUsers.splice(index, 1);
}
}
} else {
parsedUsers.unshift(user);
break;
}
}
for (const group of matchedGroups) {
const sessionId = await createSession();
for (const user of group) {
let userId = user.userId;
console.log('유저정보 -> ', user);
const socketId = userSocketMap.get(userId);
console.log(userSocketMap);
console.log('세션', sessionId);
console.log('소켓', socketId);
if (socketId) {
io.to(socketId).emit('matched', { sessionId });
await redisClient.hset(
sessionId,
userId,
JSON.stringify({
socketId: socketId,
userInterests: user.userInterests,
aiInterests: user.aiInterests,
nickname: user.nickname,
mbti: user.mbti,
question: user.question,
answer: user.answer,
})
);
await redisClient.hdel('waiting_queue', user.userId);
}
}
}
// 대기 큐 길이 업데이트를 모든 클라이언트에 전송
// const queueLength = await redisClient.hlen('waiting_queue');
throttledUpdateQueueLength();
}
});
// 대기큐 길이 변화 알림
const updateQueueLength = async () => {
const currentQueueLength = await redisClient.hlen('waiting_queue');
if (currentQueueLength !== lastQueueLength) {
lastQueueLength = currentQueueLength;
io.emit('queueLengthUpdate', currentQueueLength);
}
};
// 대기큐 길이 업데이트 (1초 제한)
const throttledUpdateQueueLength = () => {
if (!throttleTimeout) {
throttleTimeout = setTimeout(() => {
updateQueueLength();
throttleTimeout = null;
}, 1000); // 1초 간격으로 제한
}
};
const startServer = async () => {
try {
await connectDB.connectMongo();
io.on('connection', (socket) => {
socket.on(
'userDetails',
async ({
userId,
userInterests,
aiInterests,
nickname,
mbti,
question,
answer,
}) => {
if (userSocketMap.has(userId)) {
console.log('이미 연결된 유저입니다.');
socket.disconnect();
return;
}
userSocketMap.set(userId, socket.id);
console.log('유저소켓맵 -> ', userSocketMap);
socket.on('disconnect', async () => {
userSocketMap.delete(userId);
await redisClient.hdel('waiting_queue', userId);
// 유저가 대기 큐에서 삭제된 후 대기 큐 길이 업데이트 전송
throttledUpdateQueueLength();
});
console.log('유저아이디 -> ', userId);
console.log('관심사 -> ', userInterests);
const userExists = await redisClient.hexists(
'waiting_queue',
userId
);
if (!userExists) {
const userInterestsList = Array.isArray(userInterests)
? userInterests
: userInterests.split(',');
const aiInterestsList = Array.isArray(aiInterests)
? aiInterests
: aiInterests.split(',');
await redisClient.hset(
'waiting_queue',
userId,
JSON.stringify({
userId,
userInterests: userInterestsList,
aiInterests: aiInterestsList,
nickname,
mbti,
question,
answer,
})
);
const queueLength = await redisClient.hlen(
'waiting_queue'
);
console.log('대기큐 길이 -> ', queueLength);
if (queueLength % MATCH_MAKING_PERSON_NUMBERS === 0) {
redisClient.publish('matchmaking', 'match');
}
//새로운 유저가 대기 큐에 추가된 후 대기 큐 길이 업데이트 후 전송
throttledUpdateQueueLength();
} else {
console.log('유저가 이미 대기큐에 존재합니다.');
//대기 큐 길이 전송
throttledUpdateQueueLength();
}
}
);
});
server.listen(config.PORT, () => {
console.log(`Server is running on port ${config.PORT}`);
});
} catch (err) {
console.error('Failed to connect to MongoDB', err);
}
};
startServer();
export { redisClient, redisSub };