-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
108 lines (104 loc) · 2.72 KB
/
server.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
var app = require("express")();
var server = require("http").Server(app);
var io = require("socket.io")(server);
var users = [
{ username: "admin", password: "admin", isLogin: false, socketId: null }
];
var msgArr = [];
var PORT = 8888;
server.listen(PORT);
/**
* 监听客户端链接
* io是定义的服务端socket
* 回调函数是客户端的socket
* io.sockets.connected[socket.id] >> 指定id发送休息
*/
io.on("connection", socket => {
// 监听客户端的登录事件
console.log("当前socket id:", socket.id);
console.log("链接总人数:", io.eio.clientsCount);
socket.on("login", obj => {
const { username, password, isLogin } = obj;
if (!username) {
socket.emit("login", {
msg: "请登录",
type: "systemMsg",
code: 205
});
return;
}
console.log("用户登录:", username);
let user = users.find(item => item.username === username);
// 用户不存在
if (!user) {
users.push({
username,
password,
isLogin: true,
socketId: socket.id
});
socket.emit("login", {
msg: "用户注册成功",
code: 200
});
// 全局消息
io.sockets.emit("login", {
msg: `欢迎${obj.username}进入群聊`,
type: "systemMsg",
code: 200
});
} else if (password !== user.password) {
socket.emit("login", {
msg: "密码错误,登录失败",
code: 202
});
} else if (!isLogin) {
socket.emit("login", {
msg: "该用户已登录",
code: 203
});
} else {
io.sockets.emit("login", {
msg: `欢迎${obj.username}进入群聊`,
type: "systemMsg",
code: 200
});
}
console.log("所有用户", users);
});
// 发送消息
socket.on("sendMsg", obj => {
if (!obj.username) {
socket.emit("sendMsg", {
msg: "请登录",
type: "systemMsg",
code: 205
});
return;
}
let msg = Object.assign(obj, {
id: msgArr.length + 1,
socketId: socket.id
});
msgArr.push(msg);
console.log("当前信息条数:", msgArr.length);
io.sockets.emit("sendMsg", Object.assign(msg, { code: 200 }));
});
// 用户退出
socket.on("disconnect", () => {
if (users.findIndex(item => item.socketId === socket.id) > -1) {
let userIndex = users.findIndex(item => item.socketId === socket.id);
users[userIndex].isLogin = false;
socket.broadcast.emit("login", {
msg: `${users[userIndex].username}已经离开聊天室`,
type: "systemMsg",
code: 200
});
}
});
});
console.log(`服务启动成功,http://localhost:${PORT}`);
/**
* 200 正确的相应
* 201
*/