最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if Websocket is open in Javascript - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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...
}
发布评论

评论列表(0)

  1. 暂无评论