This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Java Script Websocket closes connection directly after creation
Hey I'm relatively new to the java script world,
I'm trying to send a message with a java script websocket.

var ws = new WebSocket("ws://"));
document.addEventListener('keyup', (e) => {
    //when I create the websocket here it works
    //var ws = new WebSocket("ws://")));
    ws.onopen = function(event){

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?

Gehe zu:

Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste