& Tips for Realtime Web App
should use them ;)
Jack
// 1. Authentication in Express
app.post('/admin', function(req, res) {
req.session.admin = false;
if (req.body.user === config.secret.user &&
pass(req.body.pass) === config.secret.pass) {
req.session.admin = true;
}
res.redirect('/nodefest2011.html');
});
// 2. Sharing session btw Express & Socket.IO
io.set('authorization', function(handshakeData, callback) {
if (handshakeData.headers.cookie) {
var cookie = handshakeData.headers.cookie;
var sessionID = parseCookie(cookie)['connect.sid'];
// check the express session store
sessionStore.get(sessionID, function(err, session) {
if (err) {
// not found
callback(err.message, false);
} else {
// found
handshakeData.session = session;
callback(null, true);
}
});
} else {
return callback(null, true);
// socket.io-client from node process dosen't has cookie
// return callback('Cookie dosen\'t found', false);
}
});
// 3. Authorization in Socket.IO checking handshake data
// move slide only by admin
socket.on('go', function(to) {
if (!socket.handshake.session) return false;
if (!socket.handshake.session.admin) return false;
socket.broadcast.emit('go', to);
});
// lazy broadcasting server
socket.on('review', function(data) {
socket.volatile.emit('review', data);
socket.volatile.broadcast.emit('review', data);
});
// client
socket.on('review', function(data) {
var $li = $('<li>').text(data);
$('#review').append($li);
});
// you can send dirty data to every client
// from your console
var socket = io.connect(/* path to server */);
socket.emit('review', 'm@(^_^) pugya~~~');
var readline = require('readline').createInterface(process.stdin, process.stdout);
, spawn = require('child_process').spawn,
, socket = require('socket.io-client')
, worker = null
;
rl.on('line', function(line) {
var lines = line.split(' ');
var command = lines.shift();
worker = spawn(command, lines);
worker.stdout.on('data', function(rawdata) {
console.log(data); // print to shell
socket.emit('line', data + '\n'); // send to socketserver
});
});
rl.setPrompt(prefix, prefix.length);
rl.prompt();
Don't do that with Node, Yes Emacs
function CodeStream(file) { // Not a real Stream !!
this.file = file;
this.cache = '';
events.EventEmitter.call(this);
this.readCode();
}
util.inherits(CodeStream, events.EventEmitter);
CodeStream.prototype.readCode = function() {
fs.readFile(this.file, 'utf8', function(err, data) {
if (err) {
return console.error(err);
}
if (data !== this.cache) {
this.emit('code', data);
this.cache = data;
}
setTimeout(function() {
this.readCode();
}.bind(this), 500);
}.bind(this));
};
// expected
bar
// actual
foo
bar
but we need volatile in realtime world
see more about volatile is here
simple is best
but they seems fine..
render.prototype.higlightJS = function() {
this.cache = this.cache
.replace(//g, '>')
.replace(/"(.*?)"/g, '\"$1\"')
.replace(/'(.*?)'/g, '\'$1\'')
.replace(/(var|function|static|true|false)/g, '$1')
.replace(/([A-Za-z0-9]*?)\(/g, '$1(')
.replace(/^\/\/(.*)/g, '//$1')
.replace(/[^:]\/\/(.*)/g, '//$1')
.replace(/\/\*\*(.*)/g, '/**$1')
.replace(/\*(.*)/g, '* $1')
.replace(/(\d{2,4}?)/g, '$1')
;
};
render.prototype.higlightBash = function() {
this.cache = this.cache
.replace(/(ls|cd|tree|rm|\sexpress\s|npm|\snode\s)/g, '$1')
.replace(/(create)/g, '$1')
.replace(/(Jxck\$)/g, '$1')
.replace(/(\d{4}?)/g, '$1')
;
};
// thank you ;)
/ (0)