WebSocket connection to ‘ws://xxx.xxx.192.223:xxxx/‘ failed: WebSocket opening handshake timed out

港控/mmm° 2023-02-28 12:57 94阅读 0赞

背景:

在通过 websocket 给服务器发送数据时,出现了如下错误

  1. WebSocket connection to 'ws://xxx.xxx.xxx.223:xxxx/' failed: WebSocket opening handshake timed out

20200721164759418.png

解决办法:

去除掉传统的函数调用方式,该种方式虽然可以可以实现WebSocket连接,但是再频繁的调用时,会出现需要重复的创建WebSocket对象,那样相对相对浪费资源,同时也会上述连接超时的错误

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>WebSocket</title>
  6. <script type="text/javascript">
  7. function WebSocketTest() {
  8. if ("WebSocket" in window) {
  9. alert("您的浏览器支持 WebSocket!");
  10. /*
  11. 打开一个 web socket
  12. ws://localhost:9998/ 表示 socket 连接地址
  13. echo-protocol 表示子协议
  14. */
  15. var ws = new We

发表评论

表情:
评论列表 (有 0 条评论,94人围观)

还没有评论,来说两句吧...

相关阅读