H5 wesocket

https://test.maoer123.com/wstest.html

 

 <!DOCTYPE HTML>
 <html>
 <head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>WSTest</title>
 <script type="text/javascript">
 function WebSocketTest() {
 if ("WebSocket" in window) {
 toLog("您的浏览器支持 WebSocket!");
 // 打开一个 web socket
 var ws = new WebSocket("wss://ciieapi.naviguy.com:3344/manage/websocket/WSTransferTest");
 ws.onopen = function () {
 // Web Socket 已连接上,使用 send() 方法发送数据
 ws.send(JSON.stringify({
 "operationType": "GET_DATA_UNID",
 "operationParams": {
 "unid": window.unid
 },
 "extraData": ""
 }));
 toLog("数据发送中...");
 };
 ws.onmessage = function (evt) {
 var received_msg = evt.data;
 toLog("数据已接收..." + received_msg);
 };
 ws.onclose = function () {
 // 关闭 websocket
 toLog("连接已关闭...");
 };
 } else {
 // 浏览器不支持 WebSocket
 toLog("您的浏览器不支持 WebSocket!");
 }
 }
  
 function toLog(dt) {
 var showmsg = document.getElementById("showmsg");
 showmsg.innerText = dt + "\n" + showmsg.innerText;
 }
  
 window.onload = function () {
 console.log("加载参数");
 var url = location.search; //获取url中"?“符后的字串
 if (url.indexOf("?") != -1) {
 var str = url.substr(1);
 strs = str.split("&");
 window.unid = decodeURIComponent(strs[0].replace("unid=", ""));//获取url中的unid
 console.log("unid: ", window.unid);
 }
 }
 </script>
 <style>
 .mainsee {
 background-color: coral;
 }
 </style>
 </head>
 <body>
 <div id="sse" class="mainsee">
 <h1><a href="javascript:WebSocketTest()">启动 WebSocket</a></h1>
 </div>
 <div id="showmsg">输出...</div>
 </body>
 </html>
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值