-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (55 loc) · 1.6 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var app = require('express')();
var http = require('http').Server(app)
var io = require('socket.io')(http);
var bodyparser = require('body-parser');
var connection = require('./connection');
var routes = require('./routes');
var users = {};
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
connection.init();
routes.configure(app);
io.on('connection', function(socket){
console.log('a user connected: ' + socket.id);
socket.on('chat message', function(data){
console.log(data.username + ' (' + data.email + '): ' + data.msg);
var msg = data.msg.trim();
// Los chat privados se manejarán mediante un mensaje tipo:
// data.msg = '/w destino mensaje'
if (msg.substr(0, 3) == '/w '){
msg = msg.substr(3);
var spaceIndex = msg.indexOf(' ');
var to = msg.substr(0, spaceIndex);
var msg = msg.substr(spaceIndex + 1).trim();
if (to in users) {
data = { username: data.username, msg: msg, email: data.email, id: data.id };
users[to].emit('chat message', data);
}
}
//io.emit('chat message', data);
});
socket.on('new user', function(data, callback){
if (data in users){
// holi
}
// Si el usuario no está 'logueado'
else {
socket.nickname = data;
users[socket.nickname] = socket;
updateUsers();
}
});
socket.on('disconnect', function(data){
if(!socket.nickname)
return
delete users[socket.nickname];
updateUsers();
});
function updateUsers(){
io.emit('active_users', Object.keys(users));
console.log(Object.keys(users));
}
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening on *:3000')
});