-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·289 lines (233 loc) · 7.79 KB
/
app.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
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
const express = require("express");
const path = require("path");
const favicon = require("serve-favicon");
const logger = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const moment = require("moment");
const session = require("express-session");
const compression=require("compression");
const log=require("./routes/log.js");
var app = express();
var server=require("http").createServer(app);
var datas=[];
const io = require("socket.io")(server);
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser("key"));
app.use(session({
secret: "key",
resave: false,
saveUninitialized: true
}));//这里使用session
app.use(compression());//这里添加压缩模块
app.use(express.static(path.join(__dirname, "public")));
app.use("/js",express.static(path.join(__dirname,"dist/js")));//这里添加了一个虚拟路径
app.use("/css", express.static(path.join(__dirname, "dist/css")));
app.use("/fonts", express.static(path.join(__dirname, "dist/fonts")));
app.use("/imgs", express.static(path.join(__dirname, "dist/imgs")));
app.use("/",log);
app.get("/",function(req,res)
{
if(req.session.user)
{
res.set({
"Cache-Control":"max-age=360"
});
res.sendFile(path.resolve(__dirname+"/dist/index.html"));
}
else
{
res.sendFile(path.resolve(__dirname+"/views/login.html"));
}
});
app.get("/getname",function(req,res)
{
var user=req.session.user;
var name=user.username;
res.send(JSON.stringify(name));
});
app.get("/user",function(req,res){
res.sendFile(path.resolve(__dirname+"/views/login.html"));
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
server.listen(80,function(err){
if(err)
{
console.log(err);
}
console.log("服务器已经启动");
});
Array.prototype.deleteValue=function(val)
{
for(let i=0;i<this.length;i++)
if (this[i] == val) {
this.splice(i, 1);
break;
}
};
Array.prototype.IndexOf=function(val)
{
for(let i=0;i<this.length;i++)
{
if(this[i].value==val)
{
return i;
break;
}
}
return -1;
};
//这是为了roomlist准备的
let initRoom={
valueOf:function(){
return this.value;
},
toString:function()
{
return this.value;
},
value:"hello",
datas:[]
};
let roomList=[initRoom];//这里放这全部的聊天室
io.on("connection",function(socket){
socket.on("load",function(){
socket.emit("roomlist",JSON.stringify(roomList));
datas.forEach(function(item,index){
socket.emit("serverMessage",JSON.stringify(item));
});
});//加载的时候先发送roomlist和初始化的hello
socket.on("client",function(content){
var message={
time:moment().format("YYYY-MM-DD HH:mm:ss"),
name:socket.name,
message:content
};
//这里是让用户一登录就能看见小心
if(socket.room=="hello")
{
datas.push(message);
}
socket.emit("serverMessage",JSON.stringify(message)); //发送回去
socket.to(socket.room).emit("serverMessage",JSON.stringify(message)); //发送给其他客户端
let index=roomList.IndexOf(socket.room);
if(index>=0)
{
roomList[index].datas.push(message);
}
});
socket.on("sendname",function(username){
socket.name=username;
if(!socket.room)
{
socket.join(roomList[0]);//这里是有一个默认的聊天室的
socket.room="hello";
if (!roomList[0].users) {
roomList[0].users = [].concat(username);
console.log(roomList[0].users);
} else {
roomList[0].users.push(socket.name);
}
//这里对users进行判断,如果存在就加入
console.log("添加"+roomList[0].users);
socket.to(socket.room).emit("userList",JSON.stringify(roomList[0].users));
socket.emit("userList",JSON.stringify(roomList[0].users));
}
});
socket.on("room",function(room){
});
socket.on("addroom",function(roomname)
{
let initRoom={
valueOf:function(){
return this.value;
},
toString:function()
{
return this.value;
},
value:roomname,
datas:[]
};
roomList.push(initRoom);
socket.emit("roomlist",JSON.stringify(roomList));
socket.broadcast.emit("roomlist",JSON.stringify(roomList));
});
socket.on("join",function(room){
if(socket.room=="hello")
{
roomList[0].users.deleteValue(socket.name);//这里调用自定义的方法
socket.to(socket.room).emit("userList",JSON.stringify(roomList[0].users));
socket.emit("userList",JSON.stringify(roomList[0].users));
socket.leave("hello");
}
else
{
let index=roomList.IndexOf(socket.room);
roomList[index].users.deleteValue(socket.name);//先删除自己的值
console.log(roomList[index].users+"这是user");
socket.to(socket.room).emit("userList",JSON.stringify(roomList[index].users));//然后发送给前端
// socket.emit("userList",JSON.stringify(roomList[index].users));
socket.leave(socket.room);
}
socket.join(room,()=>{
socket.room=room;//这里只是为了方便自己调用
let index=roomList.IndexOf(room);
socket.emit("roomName",room);
roomList[index].users=(roomList[index].users?roomList[index].users:[]).concat(socket.name);
console.log(roomList[index].users);
socket.emit("userList",JSON.stringify(roomList[index].users));
socket.to(socket.room).emit("userList",JSON.stringify(roomList[index].users));//这里设定是加入新房间会自动显示用户数量,但是我觉得这样不好
// let rooms = Object.keys(socket.rooms);
// console.log(rooms); // [ <socket.id>, 'room 237' ]
console.log(roomList[index].datas+" flag");
roomList[index].datas.forEach(function(item,index){
socket.emit("serverMessage",JSON.stringify(item)); //发送回去
});
});
});
socket.on("getRoomMessage",function(roomName){
let index=roomList.IndexOf(roomName);
});
socket.on("leave",function(oldroom){
if(oldroom)
{
socket.leave(oldroom,function(){
console.log("离开了 "+oldroom);
});
}
});
socket.on("getUser",function(roomName){
socket.emit();
});
socket.on("disconnect",(reason)=>{
console.log(reason);
let index=roomList.IndexOf(socket.room);
if(roomList[index])
{
roomList[index].users.deleteValue(socket.name);
socket.to(socket.room).emit("userList",JSON.stringify(roomList[index].users));
socket.emit("userList",JSON.stringify(roomList[index].users));
socket.leave(socket.room);
}
});
});