Newer
Older
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
const http = require('http');
const crypto = require('crypto');
// const ChatRoom = require('./models/chatRoom.js');
const mongoose = require('mongoose');
const ChatRoom = require('./models/chatRooms');
// 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;
console.log("join시 chatRoomId", chatRoomId);
console.log("join시 nickname", nickname);
await ChatRoom.updateOne(
{ chatRoomId },
{
$set: {
[`isOnline.${nickname}`]: true,
[`lastReadLogId.${nickname}`]: null,
},
}
);
if (!chatRooms[chatRoomId]) {
chatRooms[chatRoomId] = [];
}
const chatRoom = await ChatRoom.findOne({ chatRoomId });
console.log("join시 chatRoom", chatRoom);
if (!chatRoom) {
console.error(`ChatRoom을 찾을 수 없습니다: chatRoomId = ${chatRoomId}`);
} else {
console.log(`ChatRoom 조회 성공: ${chatRoom}`);
}
const isAlreadyParticipant = chatRoom.participants.includes(nickname);
if (!isAlreadyParticipant) {
const joinMessage = {
message: `${nickname}님이 참가했습니다.`,
timestamp: new Date(),
type: 'join'
};
chatRooms[chatRoomId].push(joinMessage);
await ChatRoom.updateOne({ chatRoomId }, {
$push: { messages: joinMessage, participants: nickname }
});
clients.forEach(client => {
client.write(constructReply(JSON.stringify(joinMessage)));
});
} else {
console.log(`${nickname}은 이미 채팅방에 참가 중입니다.`);
}
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);
});
} catch (err) {
console.error('MongoDB 채팅 메시지 저장 오류:', err);
}
} else if (type === 'leave') {
const leaveMessage = { message: `${nickname}님이 퇴장했습니다.`, timestamp: new Date() };
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();