1

在我的开发环境中,我必须代理请求以及到相应后端服务的 websocket 连接。为此,我使用lite-serverhttp-proxy-middleware

我的 lite 服务器配置文件如下所示:

var proxyMiddleware = require('http-proxy-middleware');

var oProxyOptions = {
    target: 'http://localhost:3000/api',
    ws: true,
    pathRewrite: function (path, req) { return path.replace('/api', ''); },
    changeOrigin: true
};

var oProxy = proxyMiddleware('/api', oProxyOptions);

module.exports = function(bs) {
    return {
        "port": 8000,
        "files": ["./resources/**/*.{html,htm,css,js,xml}"],
        "server": { 
            "baseDir": "./resources",
            "middleware": {
                1: oProxy
            } 
        }
    };
};

但是,我总是在控制台中收到此错误:

WebSocket connection to 'ws://localhost:8000/api/ws' failed: Connection closed before receiving a handshake response

在 lite-server 的控制台输出中,我执行以下条目:

[HPM] GET /api/ws ~> http://localhost:3000/api/ws
[HPM] Upgrading to WebSocket 

当我看到控制台输出时,我什至看到连接通过了我的节点端点。端点实现如下:

var ws  = require('ws').Server;
var wss = new ws({ server: server, path:'/api/ws' });
wss.on('connection', function connection(ws) {
    var location = url.parse(ws.upgradeReq.url, true);
    console.log("New WS connection");
}

此错误的原因可能是什么?我的配置有问题吗?

4

0 回答 0