-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
47 lines (43 loc) · 1.51 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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
users = [];
//specify the html we will use
app.use('/', express.static(__dirname + '/chatApp'));
server.listen(process.env.PORT || 3000);
//handle the socket
io.sockets.on('connection', function(socket) {
//new user login
socket.on('login', function(nickname) {
console.log("User want login:" + nickname);
if(nickname == '' || typeof(nickname) == 'undefined'){
socket.emit('error', -1);//登录失败
return;
}
if (users.indexOf(nickname) > -1) {
io.sockets.emit('error',-2);
} else {
socket.userIndex = users.length;
socket.nickname = nickname;
users.push(nickname);
socket.emit('loginSuccess');
io.sockets.emit('system', nickname, users.length, 'login');
console.log("Login person:"+ JSON.stringify(users));
};
});
//user leaves
socket.on('disconnect', function() {
users.splice(socket.userIndex, 1);
console.log("left person:"+ JSON.stringify(users));
socket.broadcast.emit('system', socket.nickname, users.length, 'logout');
});
//new message get
socket.on('postMsg', function(msg, color) {
socket.broadcast.emit('newMsg', socket.nickname, msg, color);
});
//new image get
socket.on('img', function(imgData, color) {
socket.broadcast.emit('newImg', socket.nickname, imgData, color);
});
});