-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathclient.html
More file actions
58 lines (51 loc) · 1.54 KB
/
Copy pathclient.html
File metadata and controls
58 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>服务端测试工具</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h3>服务器响应</h3>
<textarea id="response" style="width:500px;height:300px;"></textarea>
<br>
<h3>发送内容</h3>
<input id="request" type="text" style="width:200px;" value='{"cmd":"test", "module":"test", "data":{"id":"1", "name":"lee"}}'/>
<input id="send" type="button" value="发送" onclick="send();"/>
</body>
</html>
<script>
var socket = new WebSocket("ws://localhost:9000");
socket.onerror = function(event){
console.log(event);
alert("WebSocketError!");
};
// 打开Socket
socket.onopen = function(event) {
alert("连接成功!");
setInterval("heartBeat()", 5000);
// 监听消息
socket.onmessage = function(event) {
console.log('Client received a message',event);
if (event.data != "heartBeat")
{
var ta = document.getElementById("response");
ta.value += (event.data+"\n")
}
};
// 监听Socket的关闭
socket.onclose = function(event) {
console.log('Client notified socket has closed',event);
alert("与服务器断开连接!");
};
};
function send() {
socket.send(document.getElementById("request").value);
}
function heartBeat() {
socket.send("heartBeat");
}
</script>