我正在尝试创建一个 javascript 代码(它不是 node js),它每 1 秒发送一条消息,就像 ping 一样。
ws1 = new WebSocket("wss://site.com/support_ws")
console.log("[start]")
function banana(){
console.log("sending ws1")
ws1.send(`{"type":"ping","message":"[WebSocket: wss://site.com/support_ws]: ping"}`); // THIS SEND DOESN'T WORK.
}
ws1.onopen = function(event){
console.log("ws1 connected!")
ws1.send(`{"type":"ping","message":"[WebSocket: wss://site.com/support_ws]: ping"}`); // THIS SEND WORKS FINE
setInterval(banana, 1000) // every 1 second it calls function to send the ping
}
ws1.onmessage = function(event){
console.log("ws1 - " + event.data)
}
ws1.onerror = function(error) {
alert(`[error] ` + error.message);
};
但是当我尝试在功能香蕉中发送()时,我的 websocket 断开连接(每 1 秒)。我收到错误消息 WebSocket is already in CLOSING or CLOSED state 在 js 控制台上。
我想知道如何修复它以及它为什么会发生。