mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[feature] websocket support
This commit is contained in:
parent
f97c0c6167
commit
79a14acfd2
@ -1,4 +1,6 @@
|
|||||||
var WebsocketStream = require('../streams/websocket'),
|
var WebsocketStream = require('../streams/websocket'),
|
||||||
|
http = require('http'),
|
||||||
|
common = require('../common'),
|
||||||
passes = exports;
|
passes = exports;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -71,7 +73,24 @@ function XHeaders(req, socket, options) {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function stream(req, socket, options, head) {
|
function stream(req, socket, options, head) {
|
||||||
req.pipe(new WebsocketStream(options, head)).pipe(socket);
|
var r = http.request(
|
||||||
|
common.setupOutgoing(options.ssl || {}, options, req)
|
||||||
|
);
|
||||||
|
|
||||||
|
r.on('upgrade', function(res, proxySock, hd) {
|
||||||
|
if (hd && hd.length) proxySock.unshift(hd);
|
||||||
|
|
||||||
|
socket.write('HTTP/1.1 101 Switching Protocols\r\n');
|
||||||
|
socket.write(Object.keys(res.headers).map(function(i) {
|
||||||
|
return i + ": " + res.headers[i];
|
||||||
|
}).join('\r\n') + '\r\n\r\n');
|
||||||
|
proxySock.pipe(socket).pipe(proxySock);
|
||||||
|
});
|
||||||
|
|
||||||
|
r.end();
|
||||||
|
|
||||||
|
|
||||||
|
//req.pipe(new WebsocketStream(options, head)).pipe(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
] // <--
|
] // <--
|
||||||
|
|||||||
44
ttest.js
44
ttest.js
@ -1,3 +1,4 @@
|
|||||||
|
'use strict'; /* jshint node:true */
|
||||||
var caronte = require('./'),
|
var caronte = require('./'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
ws = require('ws');
|
ws = require('ws');
|
||||||
@ -7,33 +8,38 @@ var proxyTo = new ws.Server({ port: 9090 });
|
|||||||
proxyTo.on('connection', function(ws) {
|
proxyTo.on('connection', function(ws) {
|
||||||
console.log('connection!');
|
console.log('connection!');
|
||||||
ws.on('message', function(msg) {
|
ws.on('message', function(msg) {
|
||||||
console.log('received: ' + msg);
|
ws.send('ohai: ' + msg);
|
||||||
|
setTimeout(function() {
|
||||||
|
ws.send('HAHAHHA');
|
||||||
|
}, 10000);
|
||||||
});
|
});
|
||||||
ws.send('derpity?');
|
ws.send('derpity?');
|
||||||
});
|
});
|
||||||
|
|
||||||
/*caronte.createProxyServer({
|
caronte.createProxyServer({
|
||||||
ws : true,
|
ws : true,
|
||||||
target: 'http://127.0.0.1:9090'
|
target: 'http://127.0.0.1:9090'
|
||||||
}).listen(8000);*/
|
}).listen(8000);
|
||||||
|
|
||||||
|
|
||||||
var client = new ws('ws://127.0.0.1:8000');
|
var client = new ws('ws://127.0.0.1:8000');
|
||||||
client.on('open', function() {
|
client.on('open', function() {
|
||||||
client.send('baaaka');
|
client.send('baaaka');
|
||||||
console.log('sent: baaaaka');
|
console.log('sent: baaaaka');
|
||||||
|
setTimeout(function() {
|
||||||
|
client.send('cacca');
|
||||||
|
}, 5000);
|
||||||
|
client.on('message', function(msg) {
|
||||||
|
console.log('server said: ' + msg);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var srv = http.createServer(function(req, res) {
|
/*var srv = http.createServer(function(req, res) {
|
||||||
res.end('1');
|
res.end('1');
|
||||||
}).listen(8000);
|
}).listen(8000);
|
||||||
|
|
||||||
srv.on('connection', function(s) {
|
srv.on('upgrade', function(req, sock, head) {
|
||||||
s.pipe(process.stdout);
|
|
||||||
});
|
|
||||||
|
|
||||||
srv.on('upgrade', function(req, socket, head) {
|
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
port: 9090,
|
port: 9090,
|
||||||
@ -42,21 +48,17 @@ srv.on('upgrade', function(req, socket, head) {
|
|||||||
}
|
}
|
||||||
var r = http.request(options);
|
var r = http.request(options);
|
||||||
|
|
||||||
r.on('upgrade', function(res, sock, hd) {
|
r.on('upgrade', function(res, proxySock, hd) {
|
||||||
if (hd && hd.length) sock.unshift(hd);
|
if (hd && hd.length) proxySock.unshift(hd);
|
||||||
|
|
||||||
|
sock.write('HTTP/1.1 101 Switching Protocols\r\n');
|
||||||
socket.pipe(sock).pipe(socket);
|
sock.write(Object.keys(res.headers).map(function(i) {
|
||||||
//req.pipe(r).pipe(socket);
|
|
||||||
/*console.log(hd.toString('utf-8'));
|
|
||||||
var str = Object.keys(res.headers).map(function(i) {
|
|
||||||
return i + ": " + res.headers[i];
|
return i + ": " + res.headers[i];
|
||||||
}).join('\r\n');
|
}).join('\r\n') + '\r\n\r\n');
|
||||||
socket.write("HTTP/1.1 101 Switching Protocols\r\n" + str);
|
proxySock.pipe(sock).pipe(proxySock);
|
||||||
|
|
||||||
socket.write(hd);
|
|
||||||
socket.pipe(sock).pipe(socket);*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
r.end();
|
r.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user