0

我在我的 nodejs 应用程序上使用 uWebSockets,我正在尝试向 wss 服务器发送一些 http 请求。我可以通过从浏览器 url 栏访问 wss 服务器上的 GET 路由的输出来查看它们,但是当我尝试调用 PUT 、 POST 、 PATCH ......路由时,我没有得到任何输出(例如:没有响应,我想未到达路线)。

我的 uWebSockets.js 配置是以下代码片段:

const uWS = require('uWebSockets.js');
const SOCKET_PORT = 3023;

const websockets = uWS.App()

/* I tried to put these routes before and after .ws() , still no response */
.put('/route1', (req, res) => {
     console.log('/route1');
})
.patch('/route2', (req, res) => {
     console.log('/route2');
 })
.post('/route3', (req, res) => {
     console.log('/route3');
})
.delete('/route4', (req, res) => {
     console.log('/route4');
})

/* Tried with '' , '*' , '/*' but none of the attempts succeeed */
.ws('', {
     compression: uWS.SHARED_COMPRESSOR,
     idleTimeout: 30,
     maxBackpressure: 1024,
     maxPayloadLength: 512,
     open:ws=> ws.subscribe('all')
});


websockets.listen(SOCKET_PORT, (listenSocket) => {
    if (listenSocket) 
        console.log('Listening to port ' + SOCKET_PORT);
});

从前端,我尝试发送这样的请求:

    let header = { "Access-Control-Allow-Origin" : "*" , "Content-Type" : "application/json" };
    header.key= "test_key";
    await axios({
        method : "PUT",
        url : "wss://subdomain.domain.xyz/route1",
        data : { "val1" : "test" , "val2" : 5 },
        headers : header
    })
    .then(function(response){
        serverResponse = response.data;
    })
    .catch(function(err){
        serverResponse = err.response.data;
    });

我做错了 websockets 请求吗?

我正在使用 18.04 uWebSockets.js 版本。

4

1 回答 1

1

当使用 GET、POST、PATCH、PUT、DELETE 等 HTTP 方法时,您应该使用 http/https 而不是 ws/wss 进行连接。

于 2021-06-27T08:12:50.810 回答