<script>
  function WebSocketTest() {
    if ("WebSocket" in window) {
      alert("您的浏览器支持 WebSocket!");

      // 打开一个 web socket
      var ws = new WebSocket("ws://localhost:8080/");

      ws.onopen = function () {
        // Web Socket 已连接上,使用 send() 方法发送数据
        ws.send(`{"Data":"sadddddddddd"}`);
      };

      ws.onmessage = function (evt) {
        var received_msg = evt.data;
        console.log(received_msg)
      };

      ws.onclose = function () {
        // 关闭 websocket
        alert("连接已关闭...");
      };
      
    }else {
      // 浏览器不支持 WebSocket
      alert("您的浏览器不支持 WebSocket!");
    }
  }

  WebSocketTest()
</script>

webScoket测试工具

https://www.52bd.net/usr/uploads/2020/04/751993603.html

2020-04-28T07:47:20.png
WebSocketTest.html


版权声明:本文为原创文章,版权归 吾爱博客 所有,转载请联系博主获得授权!
本文地址:https://www.52bd.net/html/178.html

发表评论

正在加载 Emoji
×