Socket.io在代理后收到握手响应[ECONNRESET]之前关闭 [英] Socket.io closed before receiving a handshake response [ECONNRESET] after a proxy

查看:260
本文介绍了Socket.io在代理后收到握手响应[ECONNRESET]之前关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为node.js应用程序编程任务管理器.这些应用程序是在任意端口中打开的,因此,为了访问它们,我想伪造它们的根目录 这样的网址:

I am programming a task manager for node.js apps. These apps are opened in an arbitrary port, so, in order to reach them I want to fake their root in a URL like this:

http://domain.com/proxy/yourApplication

因此,第三方可以向其应用发出HTTP请求,而无需知道它在哪个端口运行.

So third parties could make HTTP requests to their app without knowing in which port it runs.

然后我选择了 http-proxy :

var express = require('express')
, router = express.Router()
, Helper = require('../helper')
, launcher = require('../launcher')
, httpProxy = require('http-proxy')
, proxy = httpProxy.createProxyServer({ws: true});

还有在那条路线上侦听的代码...

And the code that listens at that route...

// Proxy
router.use('/proxy/:name?', function(req, res) {

  var app, reqPath, referer, proxyUrl;

  // Capture the referer to proxy the request
  // in case the path is not clear enaugh
  if (req.get('referer') !== undefined) {
    var aux = req.get('referer').split('/');
    referer = aux[aux.indexOf('proxy') + 1]
  }

  // This block returns an app object
  // with the port where it is running
  app = launcher.getApp(req.params.name)
  || launcher.getApp(referer);

  if (app) {
    // Here app is running

    // In case the path is /proxy/:name
    // instead of /proxy/:name/ you need this block
    req.url = (req.url === '/') ? '' : req.url;
    reqPath = (referer !== undefined)
     ? '/' + req.params.name + req.url
     : req.url;
    req.url = reqPath.replace('/proxy/', '/');
    req.url = req.url.replace('/' + app.name, '');

    // This block of code actually pipes the request
    // to the running app and pass it to the client
    proxyUrl = req.protocol + '://localhost:' + app.port;
    proxy.web(req, res, { 
      target: proxyUrl
    });

    // Some logging
    console.log('req url: %s', req.url);
    console.log('proxy url: %s', proxyUrl);
    console.log('referer: %s', referer);

  } else {
    // Here app is not running
    res.status(404).json("App not running");
  }
});

对于大多数应用程序都可以正常工作,但是当使用socket.io打开应用程序时,它会提示:

It works just fine with most apps, but when opening an app with socket.io it prompts:

WebSocket connection to 'ws://localhost/proxy/xy-terminal/socket.io/1/websocket/gMqK_XRwZENuUibi4ekJ' failed: Connection closed before receiving a handshake response

在服务器控制台中...

In the server console...

Trace: { [Error: socket hang up] code: 'ECONNRESET' }
    at process.<anonymous> (/Users/jdario/Development/xy-dashboard/www:107:11)
    at process.emit (events.js:107:17)
    at process._fatalException (node.js:236:26)
    at ProxyServer.emit (/Users/jdario/Development/xy-dashboard/node_modules/http-proxy/node_modules/eventemitter3/index.js:75:35)
    at ClientRequest.proxyError (/Users/jdario/Development/xy-dashboard/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:140:16)
    at ClientRequest.emit (events.js:129:20)
    at Socket.socketCloseListener (_http_client.js:247:9)
    at Socket.emit (events.js:129:20)
    at TCP.close (net.js:485:12)

由于它是伪造应用程序根目录的代理",因此我可能无法单独修改其源代码,因此它们应按预期工作.在正确的端口(在这种情况下为3000)中打开它们时,它们没有显示任何错误.

Since it is a "proxy" that fakes an app root I may not be available to modify their source code individually, they should just work as intended. When opening them in the right port (3000 in this case) they do not show any error.

提前谢谢!

推荐答案

最短的答案是确实单独修改代理的应用程序源代码.所以现在他们使用

Shortest answer was to indeed modify the proxied apps source code individually. So now they use

var io = require('socket.io')(http, { path: '/proxy/yourApplication'})

这篇文章解决了这个问题. https://stackoverflow.com/a/31658307/2633577

This post addressed the problem. https://stackoverflow.com/a/31658307/2633577

但是,此答案不是最佳答案,因为它对托管应用程序不是100%透明.

However this answer is not the best because it is not 100% transparent to hosted apps.

这篇关于Socket.io在代理后收到握手响应[ECONNRESET]之前关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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