-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebSocketTest.html
46 lines (45 loc) · 1.32 KB
/
webSocketTest.html
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
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>WebSocket测试</title>
<script>
var ws = null;
function send() {
var message = document.getElementById("text").value;
ws.send(message);
}
function connect() {
if(ws == null)
{
ws = new WebSocket("ws://127.0.0.1:8080/ws/123");
ws.onopen = function ()
{
alert("连接成功");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
document.getElementById("showMes").value += evt.data + "\n";
};
ws.onclose = function ()
{
alert("断开了连接");
};
}
}
function closeSocket()
{
ws.close();
ws = null;
}
</script>
</head>
<body>
<input type="button" onclick="connect()" value="连接" />
<input type="button" onclick="closeSocket()" value="关闭" />
<input type="button" onclick="send()" value="发送" />
<input type="text" id="text" />
<br/>
<textarea rows="3" cols="30" id="showMes" style="width:100%;height:500px;"></textarea>
</body>
</html>