-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.js
More file actions
165 lines (137 loc) · 5.17 KB
/
chat.js
File metadata and controls
165 lines (137 loc) · 5.17 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
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
const mongoose = require("mongoose");
const Class = require("./Schemas/Class");
const User = require("./Schemas/User");
const ChatLog = mongoose.model("ChatLog", require("./Schemas/ChatLog"));
function getTime(time) {
let now = new Date();
now.setTime(time);
let timeString = "";
timeString = now.getHours() < 12 ? "오전" : "오후";
if (now.getHours() >= 13) {
timeString += " " + (now.getHours() - 12);
} else {
timeString += " " + now.getHours();
}
timeString += "." + now.getMinutes() + "." + now.getSeconds();
return timeString;
}
//TODO chat
// - 버그수정
// - room을 통한 수업별 채팅방 구현 ->수업 시작과 연동
// - 데이터베이스 연동
//TODO 잘못된 접근 대응하기
module.exports = {
getHandler: function (_io) {
const io = _io;
const handler = function (socket) {
let RoomID
// 접속한 클라이언트의 정보가 수신되면
socket.on("join", async function (data) {
if (!socket.username) {
// 전달받은 클라이언트 정보를 저장
RoomID = data.room;
socket.userID = data.userID;
//접속한 유저 찾기
User.findById(data.userID, (err, user)=>{
if(err){console.log(err); return;}
if(user == null){
//TODO 접속시도한 유저 정보 잘못되 있을 떄 대응하기
console.log("존재하지 않는 유저 입니다.")
return;
}
socket.username = user.nickname;
console.log("--- chat (" + RoomID + ")---\nlogged in : " + socket.username);
//채팅방 찾기
ChatLog.findById(RoomID, (err, found) => {
if(err){
console.log(err);
return;
}
//채팅방 생성되 있을 때만 접속 가능
if(found == null){
//TODO 채팅방 생성 안되있는데 접근할때 대응하기
console.log("존재하지 않는 채팅방 입니다.")
return;
}
// console.log("기존 메시지 전송")
//기존 채팅 데이터 전송
for(let message of found.messages){
let msgType = message.system ? 'system' : 'chat'
let m = {
system: message.system,
username: message.username,
time: getTime(message.time),
message: message.message,
}
socket.emit(msgType, m);
}
//해당 채널에 입장
socket.join(RoomID);
console.log(io.sockets.adapter.rooms);
let message = {
system: true,
message: socket.username + " 님이 채팅방 " + RoomID + " 에 입장 했습니다.",
}
// 해당 채널의 클라이언트에게 새로운 유저 입장 메시지 전송
io.sockets.in(RoomID).emit("system", message);
// DB 저장
ChatLog.findById(RoomID, (err, found)=>{
if(err){console.log(err); return;}
found.messages.push(message);
found.save();
})
})
})
}
});
// 클라이언트로부터의 메시지가 수신되면
socket.on("chat", function (data) {
console.log("Message from %s: %s", socket.username, data.message);
// 메시지를 전송한 클라이언트를 제외한 모든 클라이언트에게 메시지를 전송한다
io.sockets.in(RoomID).emit("chat", {
username: socket.username,
time: getTime(Date.now()),
message: data.message,
});
// DB 저장
ChatLog.findById(RoomID, (err, found)=>{
if(err){console.log(err); return;}
var newMessage = {
from: data.userID,
username: socket.username,
message: data.message
}
found.messages.push(newMessage);
found.save();
})
// 메시지를 전송한 클라이언트에게만 메시지를 전송한다
// socket.emit('s2c chat', msg);
// 접속된 모든 클라이언트에게 메시지를 전송한다
// io.emit('s2c chat', msg);
// 특정 클라이언트에게만 메시지를 전송한다
// io.to(id).emit('s2c chat', data);
});
// 클라이언트 퇴장시
//TODO 클라이언트 측에서 새로고침 해야만 퇴장 처리됨
socket.on("quit", function () {
console.log("user disconnected: " + socket.username);
let message = {
system: true,
message: socket.username + " 님이 퇴장 했습니다.",
}
io.emit("system", message);
// DB 저장
ChatLog.findById(RoomID, (err, found)=>{
if(err){console.log(err); return;}
found.messages.push(message);
found.save();
})
});
// force client disconnect from server
socket.on("forceDisconnect", function () {
socket.disconnect();
});
};
return handler;
},
};