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?
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?
Share Improve this question asked Apr 18, 2020 at 11:27 TransformerTransformer 411 silver badge7 bronze badges 1- Does this answer your question? How to get the current status of a javascript websocket connection – Arcanus Commented Mar 12, 2022 at 3:54
1 Answer
Reset to default 5ThereadyState
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...
}