08.11.2022, 13:23
Hey I'm relatively new to the java script world,
I'm trying to send a message with a java script websocket.
If I create the websocket variable outside of my addEventListener the websocket
will be closed immediately (ready state 3) when I try to send "TestMessage".
If I create the websocket within the addEventListener method everything works just fine.
However when I'm sending too many messages successively the connection stops.
Has someone an idea why it does not work in the first case? Is this maybe some js variable out of scope error?
I'm trying to send a message with a java script websocket.
Code:
var ws = new WebSocket("ws://192.168.179.52:8765"));
document.addEventListener('keyup', (e) => {
//when I create the websocket here it works
//var ws = new WebSocket("ws://192.168.179.52:8765")));
ws.onopen = function(event){
console.log(ws);
ws.send("TestMessage");
}
});
If I create the websocket variable outside of my addEventListener the websocket
will be closed immediately (ready state 3) when I try to send "TestMessage".
If I create the websocket within the addEventListener method everything works just fine.
However when I'm sending too many messages successively the connection stops.
Has someone an idea why it does not work in the first case? Is this maybe some js variable out of scope error?