forked from markcowl/WebChatDefault-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
98 lines (76 loc) · 1.92 KB
/
server.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
/**
* Bootstrap app.
*/
/**
* Module dependencies.
*/
var express = require('express');
var stylus = require('stylus');
var nib = require('nib');
var sio = require('socket.io');
/**
* App.
*/
var app = express.createServer();
/**
* App configuration.
*/
app.configure(function () {
app.use(app.router);
app.use(stylus.middleware({ src: __dirname + '/public', compile: compile}));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname);
app.set('view engine', 'jade');
function compile(str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
}
});
/**
* App routes.
*/
app.get('/', function (req, res) {
console.log("Setting up route using: " + req + ", " + res);
res.render('index', { layout: false });
});
/**
* App listen.
*/
var port = 3000;
if (process.env.PORT) { port = process.env.PORT;}
app.listen(port, function () {
var addr = app.address();
console.log(' app listening on http://' + addr.address + ':' + addr.port);
});
/**
* Socket.IO server (single process only)
*/
var io = sio.listen(app)
, nicknames = {};
// polling only for iis
io.configure(function() {
io.set('transports', ['xhr-polling']);
io.set('log level', 5);
});
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
socket.broadcast.emit('user message', socket.nickname, msg);
});
socket.on('nickname', function (nick, fn) {
if (nicknames[nick]) {
fn(true);
} else {
fn(false);
nicknames[nick] = socket.nickname = nick;
socket.broadcast.emit('announcement', nick + ' connected');
io.sockets.emit('nicknames', nicknames);
}
});
socket.on('disconnect', function () {
if (!socket.nickname) return;
delete nicknames[socket.nickname];
socket.broadcast.emit('announcement', socket.nickname + ' disconnected');
socket.broadcast.emit('nicknames', nicknames);
});
});