这些是我正在使用的节点和所需模块的版本:
当我运行以下程序时,我的控制台输出看起来像这样,并且不会超出这一点:
$ node app.js
proxy: got upgrade, proxying web request
wss: got connection
以下是代码:
// app.js
// A simple proxying example
//
// Setup websocket server on port 19000
// Setup proxy on port 9000 to proxy to 19000
// Make a websocket request to 9000
//
var WebSocket = require('ws'),
WebSocketServer = WebSocket.Server,
proxy = require('http-proxy');
// goes in a loop sending messages to the server as soon as
// the servers are setup
var triggerClient = function() {
var ws = new WebSocket('ws://localhost:9090/');
ws.on('open', function() {
console.log('ws: connection open');
setInterval(function() {
ws.send("Hello");
}, 1000);
});
ws.on('message', function(data) {
console.log('ws: got ' + data);
});
}
// setup websocket server and a proxy
//
var go = function() {
// setup a websocket server on port 19000
//
var wss = new WebSocketServer({ port: 19000 });
wss.on('connection', function(ws) {
console.log('wss: got connection');
ws.on('message', function(data) {
console.log('wss: got ' + data);
ws.send('wss response: ' + data);
});
});
// setup a proxy server
var server = proxy.createServer(function (req, res, proxy) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 19000
});
});
server.on('upgrade', function (req, socket, head) {
console.log('proxy: got upgrade, proxying web request');
server.proxy.proxyWebSocketRequest(req, socket, head, {
host: 'localhost',
port: 19000
});
});
server.listen(9090, triggerClient);
};
process.nextTick(go);
当我尝试使用hipache时,我的问题最终开始了,然后我将事物简化为node-http-proxy,然后最终简化为这段代码。
如果您将WebSocket客户端连接的端口从9090更改为19000(从而绕过代理),事情似乎正常。
任何建议,指示和反馈都将不胜感激。
谢谢!
答案 0 :(得分:1)
核心问题是node-http-proxy的主分支只与节点< = 0.8.x兼容(参见https://github.com/nodejitsu/node-http-proxy#when-to-use-node-http-proxy):有一棵树实现了对0.10.x的支持(参见{ {3}})但它不是主线分支,我没有找到何时合并并可用的任何指示。