-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (32 loc) · 1.08 KB
/
server.js
File metadata and controls
38 lines (32 loc) · 1.08 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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
users = [];
app.use('/', express.static(__dirname + '/www'));
server.listen(process.env.PORT || 3000);
io.sockets.on('connection', function(socket) {
socket.on('login', function (nickname) {
if (users.indexOf(nickname) > -1 ) {
socket.emit('nickExisted');
} else {
socket.userIndex = users.length;
socket.nickname = nickname;
users.push(nickname);
socket.emit('loginSuccess');
io.sockets.emit('system', nickname, users.length, 'login');
}
});
socket.on('disconnect', function () {
if (socket.nickname != null) {
users.splice(socket.userIndex,1);
socket.broadcast.emit('system', socket.nickname, users.length, 'logout');
}
});
socket.on('postMsg', function (msg, color) {
socket.broadcast.emit('newMsg', socket.nickname, msg, color);
});
socket.on('img', function (imgData, color) {
socket.broadcast.emit('newImg', socket.nickname, imgData, color);
});
});