-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
91 lines (70 loc) · 2.24 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
// Setup
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
path = require('path'),
users = [];
app.configure(function() {
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(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/', function(req, res) {
res.render('index', { title: 'Messenger.io' });
});
server.listen(app.get('port'));
io.sockets.on('connection', function(socket) {
// Login
socket.on('loginRequest', function(data) {
var error = '';
if (users.indexOf(data.name) > -1) {
error = 'Name already taken !'
} else {
users.push(data.name);
socket.set('username', data.name, function() {
socket.emit('broadcast', {
name: null,
message: 'Welcome! Press enter to start chatting.'
});
io.sockets.emit('broadcast', {
name: null,
message: data.name + ' has joined'
})
});
}
socket.emit('loginResponse', {
name: data.name,
error: error
});
});
// Message relay
socket.on('sendMessage', function(data) {
socket.get('username', function(err, name) {
data.name = name;
io.sockets.emit('broadcast', data);
});
});
// Logout
socket.on('disconnect', function() {
socket.get('username', function(err, name) {
if (name) {
io.sockets.emit('broadcast', {
name: null,
message: name + ' has disconnected'
});
((nameIndex = users.indexOf(name)) > -1) && users.splice(nameIndex, 1);
}
});
});
});