代理Websocket连接,抛出错误

时间:2018-07-31 03:12:37

标签: node.js express http-proxy

我正在尝试代理从主服务器到辅助服务器的浏览器客户端的Websocket连接。这是执行代理的代码:

var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({
  ws: true
});
var app = Express();
http_server = http.createServer(app);
http_server.on('upgrade', function(req, socket, head) {
  if (req.url == "/server/usg1") {
    if (true) //original statement returns true
    {
      proxy.ws(req, socket, head, {
        target: 'ip.com:1358' //random ip
      });
    }
  }
});

辅助服务器查看会话并决定是否允许此处的连接:

new WebSocketServer({
  port: 1358,
  verifyClient: function(info, callback) {
    sessionParser(info.req, {}, function() {
      if (info.req.session &&
        info.req.session.player &&
        OnlinePlayerManager.hasPlayer(info.req.session.player.username) == false &&
        GameManager.getPlayerGame(info.req.session.player.username) != null) {
        callback(true);
      } else {
        callback(false);
      }
    });
  }
}); 

但是,每当浏览器尝试多次连接并多次拒绝时,此错误就会在主服务器的控制台中引发,并且主服务器完全停止

events.js:167
  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
    at TCP.onread (net.js:659:25)
Emitted 'error' event at:
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

按需在主服务器上进行代理,并且在辅助服务器上正确读取了会话,但是在浏览器连接多次尝试/失败后,主服务器崩溃。作为拒绝服务的手段,这可能是灾难性的。

0 个答案:

没有答案