<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <button id="btn" type="button">点我发送请求</button>
    </body>
    <script type="text/javascript" src="jquery.min.js" ></script>
    <script type="text/javascript">
        $("#btn").click(function(){
            var url = "wss://xxxxx:8001"
            // 创建WebSocket 对象
            var wss = new WebSocket(url);
            //连接成功时,触发事件
            console.log("连接"+url+"成功")
            wss.onopen = function () {
                //请求参数
                var param = {"id": 1,"command": "account_info","account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"};
                // 使用 send() 方法发送数据
                wss.send(JSON.stringify(param));
                console.log("数据发送中...");
            }
            //接收到服务端响应的数据时,触发事件
            wss.onmessage = function (evt) {
                var data = evt.data;
                console.log("收到数据..."+data);
            }
            // 断开 web socket 连接成功触发事件
            wss.onclose = function () {
                console.log("连接已关闭...");
            };
        });
    </script>
</html>

 

相关文章:

  • 2022-12-23
  • 2021-09-04
  • 2021-11-19
  • 2022-02-10
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2021-12-09
  • 2021-12-18
  • 2022-02-08
相关资源
相似解决方案