-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (103 loc) · 3.3 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
109
110
111
112
113
114
115
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let userSessions = [];
function sendMsg(socket, sendType, msgType, msgSender, msgContent) {
let msgObj = {
msgHeader: {
sendType,
msgType
},
msgBody: {
msgSender,
msgContent,
msgDatetime: new Date()
}
};
socket.emit('chatMsg', msgObj);
}
function findSessionBySocketId(socketId) {
for (let i = 0; i < userSessions.length; i++) {
if (userSessions[i].socketId === socketId) {
return userSessions[i];
}
}
return undefined;
}
function findSessionByName(name) {
for (let i = 0; i < userSessions.length; i++) {
if (userSessions[i].name === name) {
return userSessions[i];
}
}
return undefined;
}
function removeSessionBySocketId(socketId) {
for (let i = 0; i < userSessions.length; i++) {
if (userSessions[i].socketId === socketId) {
userSessions.splice(i, 1);
}
}
}
app.get('/', function (req, res) {
res.send('<h1>vChat</h1>');
});
io.on('connection', function (socket) {
// 设置昵称 setName
socket.on('setName', function (name) {
console.log('[SESSION] setName: ' + socket.id + ', ' + name);
let session = findSessionByName(name);
if (session && session.socketId !== socket.id) {
socket.emit('setName', {success: 0, msg: '昵称已经被使用'});
} else {
findSessionBySocketId(socket.id).name = name;
socket.emit('setName', {success: 1, msg: '昵称设置成功'});
}
});
// 私聊 sayTo
socket.on('sayTo', function (data) {
// sender is exists
if (findSessionByName(data.sender)) {
let toSession;
if (toSession = findSessionByName(data.to)) {
let toSocket;
if (toSocket = io.sockets.sockets[toSession.socketId]) {
sendMsg(toSocket, 'private', 'message', data.sender, data.content);
console.log('[MSG] PRIVATE: [' + data.sender + '] to [' + data.to + '] : ' + data.content);
}
}
}
});
// 广播聊天消息 chatMsg
socket.on('chatMsg', function (data) {
// sender is exists
if (findSessionByName(data.sender)) {
// 广播: 聊天消息
sendMsg(io, 'broadcast', 'message', data.sender, data.content);
console.log('[MSG] BROADCAST: [' + data.sender + '] : ' + data.content);
}
});
// 有用户上线 - 广播在线人数
io.clients(function (error, clients) {
if (error) throw error;
userSessions.push({socketId: socket.id});
console.log('[SESSION] ADD SESSION: ' + socket.id);
// sendMsg(io, 'broadcast', 'status', 'onlineUsersCount', clients.length);
io.emit('updateConcurrentUsers', clients.length);
console.log('[STATUS] BROADCAST: [updateConcurrentUsers] : ' + clients.length);
});
// 用户下线 - 广播在线人数
socket.on('disconnect', function () {
io.clients(function (error, clients) {
if (error) throw error;
removeSessionBySocketId(socket.id);
console.log('[SESSION] REMOVE SESSION: ' + socket.id);
// sendMsg(io, 'broadcast', 'status', 'onlineUsersCount', clients.length);
io.emit('updateConcurrentUsers', clients.length);
console.log('[STATUS] BROADCAST: [updateConcurrentUsers] : ' + clients.length);
});
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});