-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
59 lines (53 loc) · 2 KB
/
chat.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
// chat.js
const chatCtrl = require("./controllers/chatCtrl");
const chatRoomCtrl = require("./controllers/chatRoomCtrl");
// :)
/*
프론트에서 채팅하기 버튼 누르면 방 생성(DB)하고 socket.connect(), 방 id로 join room
메세지 보내면 chat message 이벤트 발생, io.to(room_id).emit("chat message", msg)로 메세지 전송.
chat message 이벤트 받으면 DB에 저장하고 프론트에 전송.
나가면 leave room 하고 socket.disconnect()
*/
const setUpSocket = (io) => {
io.on("connection", (socket) => {
console.log("socket entered: " + socket.id);
console.log("A user connected\n");
io.on("disconnect", (input) => {
console.log(input.room_id);
console.log("User disconnected\n");
});
// 채팅방 id로 join room
socket.on("join room", (input) => {
console.log("Joined room: " + input.room_id+"\n");
socket.join(input.room_id);
console.log(socket.adapter.rooms);
});
// 메세지 보내면 DB에 저장하고 프론트에 전송
socket.on("send chat", (input) => {
console.log("send chat");
console.log("user_id: " + input.user_id + " msg: " + input.message+"\n");
const req = {
room_id: input.room_id,
user_id: input.user_id,
message: input.message,
}
chatCtrl.saveChat(req);
console.log("receive chat");
console.log(input.message);
socket.to(input.room_id).emit("receive chat", input.message);
});
// 나갈 때 DB에 있는 거 다 읽음 처리
socket.on("leave room", (input) => {
console.log("Left room: " + input.room_id);
chatCtrl.makeAllRead(input.room_id,input.user_id);
chatRoomCtrl.updateLastChat(input.room_id);
chatRoomCtrl.updateUnread(input.room_id, input.user1_id, input.user2_id);
socket.leave(input.room_id);
console.log(socket.adapter.rooms);
// for (const [id, socket] of io.of("/").sockets) {
// socket.disconnect();
// }
});
});
};
module.exports = setUpSocket;