http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.89k stars 1.97k forks source link

node-http-proxy and websockets #577

Open rere16 opened 10 years ago

rere16 commented 10 years ago

Hello,

I am using node-http-proxy to route websocket from port 80 to port 8080. I have a http server (port 80). I would like to create a websocket connection between the client and the server through the nodejs server. I use the following source code :

var proxy = new httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 8080
  }
});
var proxyServer = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

proxyServer.listen(80);

But, the server only receives the handshake message and the connection closes (eof). I tested with apache and proxy_mod, the server works fine.

Any idea ?

Thank you very much.

jcrugzz commented 10 years ago

@rere16 you also have to proxy the web requests in the proxyServer as websockets is initialized by a couple POST requests iirc

rere16 commented 10 years ago

Thank you very much for your answer. But, I have no idea how to do this. Please, could you give me a short example ?

Thanks

jcrugzz commented 10 years ago

@rere16 well lets start with why do you want to proxy the websocket connection to another server when you already have an http server that you can then attach the websocket server to as well?

rere16 commented 10 years ago

Because I developed an application using websocket (port 8080) and I would like that this application communicates with the client through the webserver (port 80). By proxing, only the port 80 is open. The port 8080 is local. (The webserver and the application is on the same machine)

jcrugzz commented 10 years ago

@rere16 ahh ok, I guess my argument is why are they two separate applications? The tricky part here is that you will have to custom proxy based on the particular requests that initialize the websocket connection.

rere16 commented 10 years ago

There are two applications because one is an application written in C++ using websocket and the other is the webserver (nodejs). I just want to redirect the websocket stream between the client and the application (no cache). It's a routing proxy feature.

For example with apache (port 80)

Location /ws ProxyPass ws://127.0.0.1:8080 ProxyPassReverse ws://127.0.0.1:8080 Location

The request ws://127.0.0.1:80/ws will be redirect to ws://127.0.0.1:8080/ws.

Thx

gara-MI commented 10 years ago

@rere16 I am also in this situation. I have websocket server written in c++ and generating some data and I want to use node-http-proxy. I am wondering whether you are able to find the solution for this or not.

Please let me know if you did this already Thanks

gara-MI commented 10 years ago

Did some one find the solution for this problem?

glortho commented 9 years ago

+1

rere16 commented 9 years ago

The javascript source code in my first message works. I had an issue in my C++ program.