2018-06-11 12:48:11 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
<title>测试WebSocket</title>
|
|
|
|
|
<script src="../js/jquery-3.1.1.js"></script>
|
|
|
|
|
<script language="javascript" type="text/javascript">
|
|
|
|
|
var socket;
|
2018-06-11 18:37:21 +08:00
|
|
|
|
var uri = "ws://" + window.location.host + "/WS";
|
2018-06-11 12:48:11 +08:00
|
|
|
|
var output;
|
|
|
|
|
var text = "test echo";
|
|
|
|
|
|
|
|
|
|
function write(s) {
|
|
|
|
|
var p = document.createElement("p");
|
|
|
|
|
p.innerHTML = s;
|
|
|
|
|
output.appendChild(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function doConnect() {
|
|
|
|
|
socket = new WebSocket(uri);
|
|
|
|
|
socket.onopen = function (e) { write("opened " + uri); doSend(); };
|
|
|
|
|
socket.onclose = function (e) { write("closed"); };
|
|
|
|
|
socket.onmessage = function (e) {
|
|
|
|
|
write("Received: " + e.data); //socket.close();
|
|
|
|
|
};
|
|
|
|
|
socket.onerror = function (e) { write("Error: " + e.data); };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function doSend() {
|
|
|
|
|
write("Sending: " + text);
|
|
|
|
|
//socket.send(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onInit() {
|
|
|
|
|
output = document.getElementById("output");
|
|
|
|
|
doConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.onload = onInit;
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div id="output">
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|