如何在roadhog中代理websockets?

时间:2018-08-18 13:07:21

标签: websocket

我实际上是在使用roadhog随附的dva-cli。我有一个运行在127.0.0.1:8080的Web服务(用Python / tornadoweb编写),它接受websocket连接。使用此配置(放置在.webpackrc中)转发请求:

{
        "proxy": {
        "/api/": {
            "target": "http://127.0.0.1:8080/",
            "changeOrigin": true
        }
    }
}

正常的HTTP GET请求完美地转发到服务器。但是,如果我尝试打开websocket连接,那么它将无法正常工作。前端的示例JavaScript代码:

const test = async () =>{
  await fetch('/api/'); // Normal GET request, it works through roadhog proxy
  //var socket = new WebSocket("ws://127.0.0.1:8080/api/socket"); // direct websocket request, it also works
  var socket = new WebSocket("ws:///api/socket"); // this websocket should go through the roadhog proxy, but it does not work
  socket.onmessage = (event) => { console.log("Message arrived: ", event.data); }
  socket.onopen = (event) => { socket.send("This is a test message to be sent"); }
}

roadhog中没有错误消息,并且请求未到达在ws://127.0.0.1:8080上运行的服务器

从理论上讲,我可以使用从JavaScript代码到服务器的直接URL,但是这样做也有问题。服务器检查请求的来源,如果不匹配,它将拒绝连接。我可以覆盖此(http://www.tornadoweb.org/en/stable/websocket.html#tornado.websocket.WebSocketHandler.check_origin),但是我真的很希望roadhog代替websocket请求。有可能吗?

0 个答案:

没有答案