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> | |