3

I am testing the way web sockets work in Javascript, tried the code this way:

(function(){     
   socket = new Websocket('ws://achex.ca:4010');

   socket.onopen  =  function(){
        console.log("Connection is now open")
  }
})

Nothing appears in console log...what am I doing wrong?

Transformer
  • 41
  • 1
  • 7
  • Does this answer your question? [How to get the current status of a javascript websocket connection](https://stackoverflow.com/questions/23369368/how-to-get-the-current-status-of-a-javascript-websocket-connection) – Arcanus Mar 12 '22 at 03:54

1 Answers1

5

ThereadyState property of the websocket contains the connection of the websocket at all times as specified in the WebSocket API

It will be one of the following values : CONNECTING OPEN CLOSING or CLOSED

A way to work if it's open is :

if (yourWsObject.readyState === WebSocket.OPEN) {
   // Do your stuff...
}
Sifat Haque
  • 5,357
  • 1
  • 16
  • 23