我遇到了一个与github.com/facebook/create-react-app有关的问题:
https://github.com/facebook/create-react-app/issues/8527
基本上,我正在使用自己的服务器代理webpack-dev-server。HTTP请求没有问题,但Websocket请求会失败,并显示以下错误:
WebSocket connection to 'ws://localhost:3016/sockjs-node' failed
由于3015端口上有一个Websocket服务器在监听,而3016端口上没有。
因此,我可能希望代理一个websocket请求。我可以像下面这样代理一个HTTP请求:
app.use((req,res,next) => {
// proxy to dev server
console.log('path:', req.path);
console.log('url:', req.url);
const r = http.request({
method: req.method,
path: req.path,
host: 'localhost',
protocol: 'http:',
port: 3015 // webpack-dev-server is listening on 3015
}, r => {
r.pipe(res);
});
r.once('error', next);
req.pipe(r);
});
app.listen(3016); // app listens on 3016 and forwards some requests to webpack-dev-server listening on 3015
有人知道代理websocket请求的好方法吗?
这是我尝试代理ws连接的方式:https://github.com/websockets/ws/issues/1697