-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (79 loc) · 2.76 KB
/
index.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
var app = require('http').createServer(handler).listen(process.env.PORT || 3000),
io = require('socket.io').listen(app),
fs = require('fs'),
parse = require('url').parse,
mime = require('mime'),
strokeHistory = [],
brushStyles = {},
brushStyleIdGen = 0;
io.configure('production', function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket',
// 'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
]);
});
io.configure('development', function(){
io.set('transports', ['websocket']);
});
io.sockets.on('connection', function(socket) {
// Get a new browser up to date.
socket.on('init', function(init) {
init({
brushStyles: brushStyles,
strokeHistory: strokeHistory
});
});
socket.on('registerBrushStyle', function(brushStyle, returnNewId) {
brushStyleIdGen++;
var id = brushStyle.id = brushStyleIdGen;
io.sockets.emit('newBrushStyle', brushStyle);
brushStyles[id] = brushStyle;
returnNewId(id);
});
socket.on('start', function(dot) {
io.sockets.emit('dot', dot);
strokeHistory.push(dot);
});
socket.on('move', function(segment) {
io.sockets.emit('seg', segment);
strokeHistory.push(segment);
});
socket.on('requestClear', function() {
io.sockets.emit('clear');
strokeHistory = [];
});
});
function handler (req, res) {
var url = parse(req.url);
var localPathname = (url.pathname === '/') ? '/index.html' : url.pathname;
var path = __dirname + '/public' + localPathname;
fs.stat(path, function(err, stat) {
if (err) {
if ('ENOENT' === err.code) {
res.statusCode = 404;
res.end('Not Found');
} else {
res.statusCode = 500;
res.end('Internal Server Error');
}
} else {
var type = mime.lookup(path);
var charset = mime.charsets.lookup(type);
res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
res.setHeader('Content-Length', stat.size);
var stream = fs.createReadStream(path);
stream.pipe(res);
stream.on('error', function(err) {
res.statusCode = 500;
res.end('Internal Server Error');
});
}
});
}