-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (33 loc) · 1.03 KB
/
app.js
File metadata and controls
37 lines (33 loc) · 1.03 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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require("socket.io").listen(server),
nicknames = {};
server.listen(80);
//server.listen(process.env.PORT, process.env.IP);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket) {
socket.on('send message', function(data) {
io.sockets.emit('new message', {msg: data, nick: socket.nickname});
});
socket.on('new user', function(data, callback) {
if (data in nicknames) {
callback(false);
} else {
callback(true);
socket.nickname = data;
nicknames[socket.nickname] = 1;
updateNickNames();
}
});
socket.on('disconnect', function(data) {
if(!socket.nickname) return;
delete nicknames[socket.nickname];
updateNickNames();
});
function updateNickNames() {
io.sockets.emit('usernames', nicknames);
}
});