Skip to content

Commit

Permalink
keepalive sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Sep 14, 2013
1 parent 63b016c commit dad211e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
cov
atest.js
notes
prismus-proxy.js
9 changes: 9 additions & 0 deletions lib/caronte/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ common.setupOutgoing = function(outgoing, options, req, forward) {

return outgoing;
};

common.setupSocket = function(socket) {
socket.setTimeout(0);
socket.setNoDelay(true);

socket.setKeepAlive(true, 0);

return socket;
};
4 changes: 4 additions & 0 deletions lib/caronte/passes/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ function XHeaders(req, socket, options) {
*
*/
function stream(req, socket, options, head) {
common.setupSocket(socket);

var proxyReq = http.request(
common.setupOutgoing(options.ssl || {}, options, req)
);

proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
common.setupSocket(proxySocket);

if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);

socket.write('HTTP/1.1 101 Switching Protocols\r\n');
Expand Down
50 changes: 50 additions & 0 deletions primus-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var http = require('http');
var caronte = require('./');
var Primus = require('primus');

var server = http.createServer(function (req, res) {
res.writeHead(500);
res.end('Not Implemented\n');
});

var primus = new Primus(server, { transformer: 'engine.io' });
var Socket = primus.Socket;

primus.on('error', function (err) {
console.log('Primus ' + err);
});

primus.on('connection', function (spark) {
spark.write({ from: 'server', to: 'client' });

spark.on('data', function (data) {
console.dir(data);
});
});

primus.on('disconnection', function (spark) {
console.log('disconnected');
});

server.listen(9000);

var proxy = caronte.createProxyServer({
ws: true,
target: 'http:https://localhost:9000'
});

var srv = proxy.listen(3000);

var socket = new Socket('http:https://localhost:3000');

socket.on('reconnecting', function () {
console.log('reconnecting');
});

socket.on('open', function () {
socket.write({ from: 'client', to: 'server' })
});

socket.on('data', function (data) {
console.dir(data);
});

0 comments on commit dad211e

Please sign in to comment.