happn-primus
Version:
Primus is a simple abstraction around real-time frameworks. It allows you to easily switch between different frameworks without any code changes.
63 lines (56 loc) • 1.78 kB
JavaScript
var http = require('http');
/**
* Minimum viable Browserchannel server for Node.js that works through the primus
* interface.
*
* @runat server
* @api private
*/
module.exports = function server() {
var browserchannel = require('browserchannel')
, primus = this.primus
, Spark = this.Spark;
//
// We've received a new connection, create a new Spark. The Spark will
// automatically announce it self as a new connection once it's created (after
// the next tick).
//
this.service = browserchannel.server({
base: primus.pathname
}, function connection(socket) {
var spark = new Spark(
socket.headers // HTTP request headers.
, { // IP address Location.
remoteAddress: socket.address,
remotePort: 1337
}
, socket.query // Optional query string.
, socket.id // Unique connection id.
);
spark.on('outgoing::end', function end() {
if (socket) socket.stop();
}).on('outgoing::data', function write(data) {
socket.send(data);
});
socket.on('message', spark.emits('incoming::data'));
socket.on('error', spark.emits('incoming::error'));
socket.on('close', spark.emits('incoming::end', function parser(next) {
socket.removeAllListeners();
socket = null;
next();
}));
});
//
// Listen to upgrade requests.
//
this.on('request', function request(req, res) {
//
// The browser.channel returns a middleware layer.
//
this.service(req, res, function next() {
res.writeHead(404, {'content-type': 'text/plain'});
res.end(http.STATUS_CODES[404]);
});
});
};
;