Proxied websocket connection is immediately closed

huangapple go评论131阅读模式
英文:

Proxied websocket connection is immediately closed

问题

I have a create-react-app dev server proxying backend connections (as one does). Suddenly websocket proxying stopped working.

My setupProxy.js looks like this:

const proxy = require('http-proxy-middleware');

module.exports = function(app) {
  const port = process.env.BACKEND_PORT || '8080';
  const target = `http://localhost:${port}`;
  app.use(proxy(['/path/to/socket'], {
    target,
    ws: true,
    onProxyReqWs: function(proxyReq, req, socket) {
      socket.on('error', err => console.log(err));
      console.log('socket is destroyed', socket.destroyed)
    },
    logLevel: 'debug',
  }));

  app.use(proxy(shouldProxy, {
    target,
    logLevel: 'debug',
  }));
}

(where shouldProxy is a function, since my logic for when to proxy is... non-trivial).

When the browser (Firefox 71 or Chrome 79) creates a websocket connection, I can see that the backend gets the request and responds normally, but the browser gets a 400 Bad request and the dev-server console has this:

[HPM] GET /path/to/socket -> http://localhost:8080
socket is destroyed true
[HPM] Upgrading to WebSocket
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at Socket.Writable.write (_stream_writable.js:321:17)
    at ClientRequest.<anonymous> ([...]/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:143:14)
    at ClientRequest.emit (events.js:305:20)
    at Socket.socketOnData (_http_client.js:508:11)
    at Socket.emit (events.js:305:20)
    at addChunk (_stream_readable.js:341:12)
    at readableAddChunk (_stream_readable.js:316:11)
    at Socket.Readable.push (_stream_readable.js:250:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  code: 'ERR_STREAM_DESTROYED'
}
[HPM] Client disconnected

So something seems to be destroying the socket very early in the proxying process, but I cannot fathom what.

I currently run with node 13.5.0, http-proxy 1.18.0, and http-proxy-middleware 0.20.0; I've tried downgrading node to 12.14.0 and HPM to 0.19.1, to no avail.

英文:

I have a create-react-app dev server proxying backend connections (as one does). Suddenly websocket proxying stopped working.

My setupProxy.js looks like this:

const proxy = require(&#39;http-proxy-middleware&#39;);

module.exports = function(app) {
  const port = process.env.BACKEND_PORT || &#39;8080&#39;;
  const target = `http://localhost:${port}`;
  app.use(proxy([&#39;/path/to/socket&#39;], {
    target,
    ws: true,
    onProxyReqWs: function(proxyReq, req, socket) {
      socket.on(&#39;error&#39;, err =&gt; console.log(err));
      console.log(&#39;socket is destroyed&#39;, socket.destroyed)
    },
    logLevel: &#39;debug&#39;,
  }));

  app.use(proxy(shouldProxy, {
    target,
    logLevel: &#39;debug&#39;,
  }));

(where shouldProxy is a function, since my logic for when to proxy is... non-trivial).

When the browser (Firefox 71 or Chrome 79) creates a websocket connection, I can see that the backend gets the request and responds normally, but the browser gets a 400 Bad request and the dev-server console has this:

[HPM] GET /path/to/socket -&gt; http://localhost:8080
socket is destroyed true
[HPM] Upgrading to WebSocket
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at Socket.Writable.write (_stream_writable.js:321:17)
    at ClientRequest.&lt;anonymous&gt; ([...]/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:143:14)
    at ClientRequest.emit (events.js:305:20)
    at Socket.socketOnData (_http_client.js:508:11)
    at Socket.emit (events.js:305:20)
    at addChunk (_stream_readable.js:341:12)
    at readableAddChunk (_stream_readable.js:316:11)
    at Socket.Readable.push (_stream_readable.js:250:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  code: &#39;ERR_STREAM_DESTROYED&#39;
}
[HPM] Client disconnected

So something seems to be destroying the socket very early in the proxying process, but I cannot fathom what.

I currently run with node 13.5.0, http-proxy 1.18.0 and http-proxy-middleware 0.20.0; I've tried downgrading node to 12.14.0 and HPM to 0.19.1, to no avail.

答案1

得分: 3

这是一个关于create-react-app 3.3.0的问题1,由webpack-dev-server中的这个bug引起2。将"webpack-dev-server": "3.10.1"添加到package.jsonresolutions部分,以及将SKIP_PREFLIGHT_CHECK=true添加到.env中可以解决它。

英文:

This was an issue with create-react-app 3.3.0, caused by this bug in webpack-dev-server. Adding &quot;webpack-dev-server&quot;: &quot;3.10.1&quot; to the resolutions section of package.json and SKIP_PREFLIGHT_CHECK=true to .env fixed it.

huangapple
  • 本文由 发表于 2020年1月3日 21:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定