Skip to content

Commit dad211e

Browse files
committed
keepalive sockets
1 parent 63b016c commit dad211e

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
cov
44
atest.js
55
notes
6+
prismus-proxy.js

lib/caronte/common.js

+9
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
3333

3434
return outgoing;
3535
};
36+
37+
common.setupSocket = function(socket) {
38+
socket.setTimeout(0);
39+
socket.setNoDelay(true);
40+
41+
socket.setKeepAlive(true, 0);
42+
43+
return socket;
44+
};

lib/caronte/passes/ws.js

+4
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ function XHeaders(req, socket, options) {
7272
*
7373
*/
7474
function stream(req, socket, options, head) {
75+
common.setupSocket(socket);
76+
7577
var proxyReq = http.request(
7678
common.setupOutgoing(options.ssl || {}, options, req)
7779
);
7880

7981
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
82+
common.setupSocket(proxySocket);
83+
8084
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
8185

8286
socket.write('HTTP/1.1 101 Switching Protocols\r\n');

primus-proxy.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var http = require('http');
2+
var caronte = require('./');
3+
var Primus = require('primus');
4+
5+
var server = http.createServer(function (req, res) {
6+
res.writeHead(500);
7+
res.end('Not Implemented\n');
8+
});
9+
10+
var primus = new Primus(server, { transformer: 'engine.io' });
11+
var Socket = primus.Socket;
12+
13+
primus.on('error', function (err) {
14+
console.log('Primus ' + err);
15+
});
16+
17+
primus.on('connection', function (spark) {
18+
spark.write({ from: 'server', to: 'client' });
19+
20+
spark.on('data', function (data) {
21+
console.dir(data);
22+
});
23+
});
24+
25+
primus.on('disconnection', function (spark) {
26+
console.log('disconnected');
27+
});
28+
29+
server.listen(9000);
30+
31+
var proxy = caronte.createProxyServer({
32+
ws: true,
33+
target: 'http://localhost:9000'
34+
});
35+
36+
var srv = proxy.listen(3000);
37+
38+
var socket = new Socket('http://localhost:3000');
39+
40+
socket.on('reconnecting', function () {
41+
console.log('reconnecting');
42+
});
43+
44+
socket.on('open', function () {
45+
socket.write({ from: 'client', to: 'server' })
46+
});
47+
48+
socket.on('data', function (data) {
49+
console.dir(data);
50+
});

0 commit comments

Comments
 (0)