问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

WebSocket Test

创作时间:
作者:
@小白创作中心

WebSocket Test

引用
1
来源
1.
https://geek-docs.com/websocket/websocket-tutorials/websockets-handling-errors.html

WebSocket在客户端和服务器之间建立连接后,会触发open事件。在通信过程中发生的错误会通过onerror事件进行标记和处理。一旦发生错误,连接就会终止。

当通信过程中发生错误时,会触发onerror事件。在onerror事件之后,连接会终止,这通常表现为一个关闭事件。

良好的实践是在发生意外错误时始终通知用户,并尝试重新连接。

socket.onclose = function(event) {
   console.log("Error occurred.");

   // Inform the user about the error.
   var label = document.getElementById("status-label");
   label.innerHTML = "Error: " + event;
}

在处理错误时,需要考虑内部和外部参数:

  • 内部参数包括由于代码中的错误或意外的用户行为而可能生成的错误。
  • 外部错误与应用程序无关;它们与网络连接等无法控制的参数有关。
  • 任何交互式双向Web应用程序都需要有效的Internet连接。

检查网络可用性

想象一下,当用户正在使用网络应用时,突然网络连接在任务中间变得无法响应。在现代本机桌面和移动应用程序中,检查网络可用性是一项常见任务。

最常见的方法是向一个应该启动的网站发出HTTP请求(例如,http://www.google.com)。如果请求成功,则桌面或移动设备知道存在活动连接。类似地,HTML可使用`XMLHttpRequest`来确定网络可用性。

但是,HTML5使它变得更加容易,并引入了一种检查浏览器是否可以接受Web响应的方法。这是通过导航器对象实现的:

if (navigator.onLine) {
   alert("You are Online");
}else {
   alert("You are Offline");
}

离线模式表示设备未连接或用户已从浏览器工具栏中选择离线模式。以下是如何通知用户网络不可用并尝试在发生WebSocket关闭事件时重新连接:

socket.onclose = function (event) {
   // Connection closed.
   // Firstly, check the reason.

   if (event.code != 1000) {
      // Error code 1000 means that the connection was closed normally.
      // Try to reconnect.

      if (!navigator.onLine) {
         alert("You are offline. Please connect to the Internet and try again.");
      }
   }
}

接收错误消息的示例

以下程序说明如何使用WebSocket显示错误消息:

<!DOCTYPE html>
<html>
   <meta charset = "utf-8" />
   <title>WebSocket Test</title>

   <script language = "javascript" type = "text/javascript">
      var wsUri = "ws://echo.websocket.org/";
      var output;

      function init() {
         output = document.getElementById("output");
         testWebSocket();
      }

      function testWebSocket() {
         websocket = new WebSocket(wsUri);

         websocket.onopen = function(evt) {
            onOpen(evt)
         };

         websocket.onclose = function(evt) {
            onClose(evt)
         };

         websocket.onerror = function(evt) {
            onError(evt)
         };
      }

      function onOpen(evt) {
         writeToScreen("CONNECTED");
         doSend("WebSocket rocks");
      }

      function onClose(evt) {
         writeToScreen("DISCONNECTED");
      }

      function onError(evt) {
         writeToScreen('<span style = "color: red;">ERROR:</span> ' + evt.data);
      } 

      function doSend(message) {
         writeToScreen("SENT: " + message); websocket.send(message);
      }

      function writeToScreen(message) {
         var pre = document.createElement("p"); 
         pre.style.wordWrap = "break-word"; 
         pre.innerHTML = message; output.appendChild(pre);
      }

      window.addEventListener("load", init, false);
   </script>

   <h2>WebSocket Test</h2>
   <div id = "output"></div>

</html>

在浏览器中打开上面代码文件,得到以下结果:

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号