-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (99 loc) · 3.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, chat = require('./routes/chat')
, socketio = require('socket.io')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/chat', chat.main);
app.get('/users', user.list);
var server = app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socketio.listen(server);
var clients = {};
var socketsOfClients = {};
io.sockets.on('connection', function(socket) {
socket.on('set username', function(userName) {
// Is this an existing user name?
if (clients[userName] === undefined) {
// Does not exist ... so, proceed
clients[userName] = socket.id;
socketsOfClients[socket.id] = userName;
userNameAvailable(socket.id, userName);
userJoined(userName);
} else
if (clients[userName] === socket.id) {
// Ignore for now
} else {
userNameAlreadyInUse(socket.id, userName);
}
});
socket.on('message', function(msg) {
var srcUser;
if (msg.inferSrcUser) {
// Infer user name based on the socket id
srcUser = socketsOfClients[socket.id];
} else {
srcUser = msg.source;
}
if (msg.target == "All") {
// broadcast
io.sockets.emit('message',
{"source": srcUser,
"message": msg.message,
"target": msg.target});
} else {
// Look up the socket id
io.sockets.sockets[clients[msg.target]].emit('message',
{"source": srcUser,
"message": msg.message,
"target": msg.target});
}
})
socket.on('disconnect', function() {
var uName = socketsOfClients[socket.id];
delete socketsOfClients[socket.id];
delete clients[uName];
// relay this message to all the clients
userLeft(uName);
})
})
function userJoined(uName) {
Object.keys(socketsOfClients).forEach(function(sId) {
io.sockets.sockets[sId].emit('userJoined', { "userName": uName });
})
}
function userLeft(uName) {
io.sockets.emit('userLeft', { "userName": uName });
}
function userNameAvailable(sId, uName) {
setTimeout(function() {
console.log('Sending welcome msg to ' + uName + ' at ' + sId);
io.sockets.sockets[sId].emit('welcome', { "userName" : uName, "currentUsers": JSON.stringify(Object.keys(clients)) });
}, 500);
}
function userNameAlreadyInUse(sId, uName) {
setTimeout(function() {
io.sockets.sockets[sId].emit('error', { "userNameInUse" : true });
}, 500);
}