-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (37 loc) · 1.15 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var users = {};
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('user connected', function(msg) {
if (!users[socket.id]) {
socket.broadcast.emit('user connected', msg);
} else {
var text = users[socket.id].nick + ' now is ' + msg;
socket.broadcast.emit('change nick', text);
}
users[socket.id] = {nick: msg};
});
socket.on('disconnect', function(msg){
// This 'if' prevents crash errors when you reload the page in localhost
if (users[socket.id]) {
msg = users[socket.id].nick;
socket.broadcast.emit('user disconnected', msg);
delete users[socket.id];
}
});
socket.on('chat message', function(msg){
socket.broadcast.emit('chat message', msg);
});
socket.on('typing', function(msg){
msg = users[socket.id].nick;
socket.broadcast.emit('typing', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});