我在我的nodejs应用程序上使用uWebSockets,并且我试图向wss服务器发送一些http请求。我可以通过浏览器url bar访问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版本。
发布于 2021-06-27 16:12:51
在使用GET、POST、PATCH、PUT、DELETE等HTTP方法时,您应该使用http/https而不是ws/wss进行连接。
https://stackoverflow.com/questions/66907856
复制相似问题