使用节点http-proxy代理Websocket连接 [英] Using node http-proxy to proxy websocket connections

查看:1234
本文介绍了使用节点http-proxy代理Websocket连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过socket.io使用websockets的应用程序.对于我的应用程序,我想使用一个单独的HTTP服务器来为我的应用程序提供静态内容和JavaScript.因此,我需要放置一个代理.

I have an application that uses websockets via socket.io. For my application I would like to use a separate HTTP server for serving the static content and JavaScript for my application. Therefore, I need to put a proxy in place.

我正在使用 node-http-proxy .首先,我要在端口8081上运行我的websockets应用程序.我正在使用以下代码将socket.io通信重定向到该独立服务器,同时使用express服务静态内容:

I am using node-http-proxy. As a starting point I have my websockets app running on port 8081. I am using the following code to re-direct socket.io communications to this standalone server, while using express to serve the static content:

var http = require('http'),
    httpProxy = require('http-proxy'),
    express = require('express');

// create a server
var app = express();
var proxy = httpProxy.createProxyServer({ ws: true });

// proxy HTTP GET / POST
app.get('/socket.io/*', function(req, res) {
  console.log("proxying GET request", req.url);
  proxy.web(req, res, { target: 'http://localhost:8081'});
});
app.post('/socket.io/*', function(req, res) {
  console.log("proxying POST request", req.url);
  proxy.web(req, res, { target: 'http://localhost:8081'});
});

// Proxy websockets
app.on('upgrade', function (req, socket, head) {
  console.log("proxying upgrade request", req.url);
  proxy.ws(req, socket, head);
});

// serve static content
app.use('/', express.static(__dirname + "/public"));

app.listen(8080);

上面的应用程序运行良好,但是,我可以看到socket.io不再使用websockets,而是转而使用XHR轮询.

The above application works just fine, however, I can see that socket.io is no longer using websockets, it is instead falling back to XHR polling.

我可以通过查看代理代码中的日志来确认这一点:

I can confirm that by looking at the logs from the proxy code:

proxying GET request /socket.io/1/?t=1391781619101
proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294
proxying POST request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=1391781629
proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294
proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294

有人知道如何代理Web套接字通信吗? node-http-proxy中的所有示例均假定您要代理所有流量,而不是代理某些流量并为其提供服务.

Does anyone know how to proxy the web sockets communication? All the examples from node-http-proxy assume that you want to proxy all traffic, rather than proxying some and serving others.

推荐答案

只是偶然发现了您的问题,我发现它仍然没有得到回答.好吧,如果您仍在寻找解决方案... 您的代码中的问题是app.listen(8080)只是语法糖

Just stumbled upon your question, and I see that it is still not answered. Well, in case you are still looking for the solution... The problem in your code is that app.listen(8080) is just syntactic sugar for

require('http').createServer(app).listen(8080)

app本身只是一个处理函数,而不是httpServer的实例(我个人认为应该从Express中删除此功能,以避免造成混淆). 因此,实际上您的app.on('upgrade')从未使用过.您应该改写

while app itself is just a handler function, not an instance of httpServer (I personally believe that this feature should be removed from Express to avoid confusion). Thus, your app.on('upgrade') is actually never used. You should instead write

var server = require('http').createServer(app);
server.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});
server.listen(8080);

希望,会有所帮助.

这篇关于使用节点http-proxy代理Websocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆