Skip to content

Commit 79a14ac

Browse files
committed
[feature] websocket support
1 parent f97c0c6 commit 79a14ac

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

lib/caronte/passes/ws.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var WebsocketStream = require('../streams/websocket'),
2+
http = require('http'),
3+
common = require('../common'),
24
passes = exports;
35

46
/*!
@@ -71,7 +73,24 @@ function XHeaders(req, socket, options) {
7173
*
7274
*/
7375
function stream(req, socket, options, head) {
74-
req.pipe(new WebsocketStream(options, head)).pipe(socket);
76+
var r = http.request(
77+
common.setupOutgoing(options.ssl || {}, options, req)
78+
);
79+
80+
r.on('upgrade', function(res, proxySock, hd) {
81+
if (hd && hd.length) proxySock.unshift(hd);
82+
83+
socket.write('HTTP/1.1 101 Switching Protocols\r\n');
84+
socket.write(Object.keys(res.headers).map(function(i) {
85+
return i + ": " + res.headers[i];
86+
}).join('\r\n') + '\r\n\r\n');
87+
proxySock.pipe(socket).pipe(proxySock);
88+
});
89+
90+
r.end();
91+
92+
93+
//req.pipe(new WebsocketStream(options, head)).pipe(socket);
7594
}
7695

7796
] // <--

ttest.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict'; /* jshint node:true */
12
var caronte = require('./'),
23
http = require('http'),
34
ws = require('ws');
@@ -7,33 +8,38 @@ var proxyTo = new ws.Server({ port: 9090 });
78
proxyTo.on('connection', function(ws) {
89
console.log('connection!');
910
ws.on('message', function(msg) {
10-
console.log('received: ' + msg);
11+
ws.send('ohai: ' + msg);
12+
setTimeout(function() {
13+
ws.send('HAHAHHA');
14+
}, 10000);
1115
});
1216
ws.send('derpity?');
1317
});
1418

15-
/*caronte.createProxyServer({
19+
caronte.createProxyServer({
1620
ws : true,
1721
target: 'http://127.0.0.1:9090'
18-
}).listen(8000);*/
22+
}).listen(8000);
1923

2024

2125
var client = new ws('ws://127.0.0.1:8000');
2226
client.on('open', function() {
2327
client.send('baaaka');
2428
console.log('sent: baaaaka');
29+
setTimeout(function() {
30+
client.send('cacca');
31+
}, 5000);
32+
client.on('message', function(msg) {
33+
console.log('server said: ' + msg);
34+
});
2535
});
2636

2737

28-
var srv = http.createServer(function(req, res) {
38+
/*var srv = http.createServer(function(req, res) {
2939
res.end('1');
3040
}).listen(8000);
3141
32-
srv.on('connection', function(s) {
33-
s.pipe(process.stdout);
34-
});
35-
36-
srv.on('upgrade', function(req, socket, head) {
42+
srv.on('upgrade', function(req, sock, head) {
3743
3844
var options = {
3945
port: 9090,
@@ -42,21 +48,17 @@ srv.on('upgrade', function(req, socket, head) {
4248
}
4349
var r = http.request(options);
4450
45-
r.on('upgrade', function(res, sock, hd) {
46-
if (hd && hd.length) sock.unshift(hd);
51+
r.on('upgrade', function(res, proxySock, hd) {
52+
if (hd && hd.length) proxySock.unshift(hd);
4753
48-
49-
socket.pipe(sock).pipe(socket);
50-
//req.pipe(r).pipe(socket);
51-
/*console.log(hd.toString('utf-8'));
52-
var str = Object.keys(res.headers).map(function(i) {
54+
sock.write('HTTP/1.1 101 Switching Protocols\r\n');
55+
sock.write(Object.keys(res.headers).map(function(i) {
5356
return i + ": " + res.headers[i];
54-
}).join('\r\n');
55-
socket.write("HTTP/1.1 101 Switching Protocols\r\n" + str);
56-
57-
socket.write(hd);
58-
socket.pipe(sock).pipe(socket);*/
57+
}).join('\r\n') + '\r\n\r\n');
58+
proxySock.pipe(sock).pipe(proxySock);
5959
});
6060
6161
r.end();
6262
});
63+
64+
*/

0 commit comments

Comments
 (0)