-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (34 loc) · 1.17 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
/**
* @author Nils Dehl <[email protected]>
*/
var http = require('http'),
sys = require('sys'),
fs = require('fs'),
io = require('socket.io');
var Connect = require('connect');
var server = Connect.createServer(
Connect.logger(), // Log responses to the terminal using Common Log Format.
//Connect.responseTime(), // Add a special header with timing information.
Connect.conditionalGet(), // Add HTTP 304 responses to save even more bandwidth.
Connect.cache(), // Add a short-term ram-cache to improve performance.
Connect.gzip(), // Gzip the output stream when the browser wants it.
Connect.staticProvider(__dirname) // Serve all static files in the current dir.
);
var socket = io.listen(server);
socket.on('connection', function(client) {
var user;
client.on('message', function(message) {
if (!user) {
user = message;
client.send({ message: 'Welcome, ' + user.nickname + '!', nickname: 'server', gravatar: '' });
return;
}
var response = {
'nickname': user.nickname,
'gravatar': user.gravatar,
'message': message.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")
};
socket.broadcast(response);
});
});
server.listen(4000);