-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (28 loc) · 1017 Bytes
/
index.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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var people = {};
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
people[socket.id] = 'Guest';
io.emit('print', 'A guest has connected');
io.emit('update online list', people);
socket.on('disconnect', function () {
io.emit('print', people[socket.id] + ' has disconnected');
delete people[socket.id];
io.emit('update online list', people);
});
socket.on('chat message', function (msg) {
io.emit('print', people[socket.id] + ' says: ' + msg);
});
socket.on('set nick', function (name) {
io.emit('print', (people[socket.id] || 'Guest') + ' is now called ' + name);
people[socket.id] = name;
io.emit('update online list', people);
});
});
http.listen(3000, function () {
console.log('listening on http://localhost:3000');
});