英文:
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('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.
答案1
得分: 3
这是一个关于create-react-app 3.3.0的问题1,由webpack-dev-server中的这个bug引起2。将"webpack-dev-server": "3.10.1"
添加到package.json
的resolutions
部分,以及将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 "webpack-dev-server": "3.10.1"
to the resolutions
section of package.json
and SKIP_PREFLIGHT_CHECK=true
to .env
fixed it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论