Newer
Older
const http = require('http');
const crypto = require('crypto');
// const ChatRoom = require('./models/chatRoom.js');
const mongoose = require('mongoose');
const admin = require('firebase-admin');
const dotenv = require('dotenv');
// .env 파일 로드
dotenv.config();
// 서비스 계정 키 파일 경로를 환경 변수에서 가져오기
const serviceAccountPath = process.env.FIREBASE_CREDENTIAL_PATH;
// Firebase Admin SDK 초기화
admin.initializeApp({
credential: admin.credential.cert(require(serviceAccountPath)),
});
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
// WebSocket 관련 데이터
let clients = [];
let chatRooms = {};
// MongoDB 연결 설정
async function connectMongoDB() {
try {
await mongoose.connect('mongodb://localhost:27017/chat', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB에 성공적으로 연결되었습니다.');
// MongoDB 연결 성공 후 WebSocket 서버 시작
startWebSocketServer();
} catch (err) {
console.error('MongoDB 연결 실패:', err);
process.exit(1);
}
}
// 채팅방 기록 불러오기 함수
async function getChatHistory(chatRoomId) {
const chatRoom = await ChatRoom.findOne({ chatRoomId });
return chatRoom ? chatRoom.messages : [];
}
// WebSocket 서버 생성 및 핸드셰이크 처리
function startWebSocketServer() {
const wsServer = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('WebSocket server is running');
});
wsServer.on('upgrade', (req, socket, head) => {
const key = req.headers['sec-websocket-key'];
const acceptKey = generateAcceptValue(key);
const responseHeaders = [
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${acceptKey}`
];
socket.write(responseHeaders.join('\r\n') + '\r\n\r\n');
// 클라이언트를 clients 배열에 추가
clients.push(socket);
let chatRoomId = null;
let nickname = null;
socket.on('data', async buffer => {
let message;
try {
message = parseMessage(buffer);
const parsedData = JSON.parse(message);
const { type, chatRoomId: clientChatRoomId, nickname: clientNickname, text } = parsedData;
console.log('서버에서 수신한 메시지:', { type, clientChatRoomId, clientNickname, text });
if (type === 'join' || type === 'leave') {
await ChatRoom.updateOne(
{ chatRoomId: clientChatRoomId },
{ $set: { [`isOnline.${clientNickname}`]: type === 'join' } }
);
const statusMessage = {
type: 'status',
chatRoomId: clientChatRoomId,
nickname: clientNickname,
isOnline: type === 'join',
};
clients.forEach(client => {
client.write(constructReply(JSON.stringify(statusMessage)));
});
}
if (type === 'join') {
chatRoomId = clientChatRoomId;
nickname = clientNickname;
await ChatRoom.updateOne(
{ chatRoomId },
{
$set: {
[`isOnline.${nickname}`]: true,
[`lastReadLogId.${nickname}`]: null,
},
}
);
if (!chatRooms[chatRoomId]) {
chatRooms[chatRoomId] = [];
}
const chatRoom = await ChatRoom.findOne({ chatRoomId });
// 참가자 확인
const participantIndex = chatRoom.participants.findIndex(participant => participant.name === nickname);
if (participantIndex !== -1) {
const existingParticipant = chatRoom.participants[participantIndex];
// 참가자 상태 업데이트
existingParticipant.isOnline = true;
existingParticipant.lastReadAt = new Date();
await chatRoom.save();
} else {
// 새 참가자 추가
const joinMessage = {
message: `${nickname}님이 참가했습니다.`,
timestamp: new Date(),
type: 'join'
};
chatRoom.participants.push({
name: nickname,
fcmTokens: parsedData.fcmToken ? [parsedData.fcmToken] : [],
lastReadAt: new Date(),
lastReadLogId: null,
isOnline: true,
chatRoom.messages.push(joinMessage);
await chatRoom.save();
clients.forEach(client => {
client.write(constructReply(JSON.stringify(joinMessage)));
});
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
}
try {
const previousMessages = await getChatHistory(chatRoomId);
if (previousMessages.length > 0) {
socket.write(constructReply(JSON.stringify({ type: 'previousMessages', messages: previousMessages })));
console.log(`이전 메시지 전송: ${previousMessages.length}개`);
}
} catch (err) {
console.error('이전 채팅 기록 불러오기 중 오류 발생:', err);
}
} else if (type === 'message') {
const chatMessage = {
message: text,
timestamp: new Date(),
type: 'message',
sender: nickname
};
chatRooms[chatRoomId].push(chatMessage);
try {
// 새로운 메시지를 messages 배열에 추가
const updatedChatRoom = await ChatRoom.findOneAndUpdate(
{ chatRoomId },
{ $push: { messages: chatMessage } },
{ new: true, fields: { "messages": { $slice: -1 } } } // 마지막 추가된 메시지만 가져옴
);
// 마지막에 추가된 메시지의 _id를 가져오기
const savedMessage = updatedChatRoom.messages[updatedChatRoom.messages.length - 1];
// 새로운 메시지 전송: 클라이언트로 메시지 브로드캐스트
const messageData = {
type: 'message',
chatRoomId,
sender: nickname,
message: text,
timestamp: chatMessage.timestamp,
_id: savedMessage._id // 저장된 메시지의 _id 사용
};
clients.forEach(client => {
client.write(constructReply(JSON.stringify(messageData)));
console.log('채팅 메시지 전송:', messageData);
});
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
// 오프라인 사용자에게 FCM 푸시 알림 전송
const chatRoom = await ChatRoom.findOne({ chatRoomId });
const offlineParticipants = chatRoom.participants.filter(participant => {
// isOnline 상태를 Map에서 가져오기
const isOnline = chatRoom.isOnline.get(participant.name);
return isOnline === false; // 정확히 false인 사용자만 필터링
});
for (const participant of offlineParticipants) {
const tokens = participant.fcmTokens || [];
// console("푸시 알림 보내는 토큰", tokens);
if (tokens.length > 0) {
const message = {
tokens, // FCM 토큰 배열
notification: {
title: `${chatRoom.chatRoomName}`,
body: `${nickname}: ${text}`,
},
data: {
key1: 'value1',
key2: 'value2',
},
android: {
priority: 'high',
},
apns: {
payload: {
aps: {
sound: 'default',
},
},
},
};
try {
console.log(`푸시 알림 전송 중 (${participant.name}):`, message); // 디버깅 로그 추가
const response = await admin.messaging().sendEachForMulticast(message);
console.log(`푸시 알림 전송 성공 (${participant.name}):`, response.successCount);
} catch (error) {
console.error(`푸시 알림 전송 실패 (${participant.name}):`, error);
}
} else {
console.log(`사용자 ${participant.name}의 FCM 토큰이 없습니다.`);
}
}
} catch (err) {
console.error('MongoDB 채팅 메시지 저장 오류:', err);
}
} else if (type === 'leave') {
const leaveMessage = {
message: `${nickname}님이 퇴장했습니다.`,
timestamp: new Date(),
type: 'leave'
};
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
chatRooms[chatRoomId].push(leaveMessage);
await ChatRoom.updateOne(
{ chatRoomId },
{ $set: { [`isOnline.${nickname}`]: false } }
);
await ChatRoom.updateOne({ chatRoomId }, {
$push: { messages: leaveMessage },
$pull: { participants: nickname }
});
clients.forEach(client => {
client.write(constructReply(JSON.stringify(leaveMessage)));
});
clients = clients.filter(client => client !== socket);
}
} catch (err) {
console.error('메시지 처리 중 오류 발생:', err);
}
});
socket.on('close', async () => {
if (nickname && chatRoomId) {
await ChatRoom.updateOne(
{ chatRoomId },
{ $set: { [`isOnline.${nickname}`]: false } }
);
const statusMessage = {
type: 'status',
chatRoomId,
nickname,
isOnline: false,
};
clients.forEach(client => {
client.write(constructReply(JSON.stringify(statusMessage)));
});
}
});
socket.on('error', (err) => {
console.error(`WebSocket error: ${err}`);
clients = clients.filter(client => client !== socket);
});
});
wsServer.listen(8081, () => {
console.log('WebSocket 채팅 서버가 8081 포트에서 실행 중입니다.');
});
}
// Sec-WebSocket-Accept 헤더 값 생성 -> env처리
function generateAcceptValue(key) {
return crypto.createHash('sha1').update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', 'binary').digest('base64');
}
// WebSocket 메시지 파싱 함수
function parseMessage(buffer) {
const byteArray = [...buffer];
const secondByte = byteArray[1];
let length = secondByte & 127;
let maskStart = 2;
if (length === 126) {
length = (byteArray[2] << 8) + byteArray[3];
maskStart = 4;
} else if (length === 127) {
length = 0;
for (let i = 0; i < 8; i++) {
length = (length << 8) + byteArray[2 + i];
}
maskStart = 10;
}
const dataStart = maskStart + 4;
const mask = byteArray.slice(maskStart, dataStart);
const data = byteArray.slice(dataStart, dataStart + length).map((byte, i) => byte ^ mask[i % 4]);
return new TextDecoder('utf-8').decode(Uint8Array.from(data));
}
// 클라이언트 메시지 응답 생성 함수
function constructReply(message) {
const messageBuffer = Buffer.from(message, 'utf-8');
const length = messageBuffer.length;
const reply = [0x81];
if (length < 126) {
reply.push(length);
} else if (length < 65536) {
reply.push(126, (length >> 8) & 255, length & 255);
} else {
reply.push(
127,
(length >> 56) & 255,
(length >> 48) & 255,
(length >> 40) & 255,
(length >> 32) & 255,
(length >> 24) & 255,
(length >> 16) & 255,
(length >> 8) & 255,
length & 255
);
}
return Buffer.concat([Buffer.from(reply), messageBuffer]);
}
// MongoDB 연결 후 WebSocket 서버 시작
connectMongoDB();