-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket控制.html
55 lines (46 loc) · 1.13 KB
/
websocket控制.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
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebSocket</title>
<style type="text/css">
body {
text-align: center;
zoom: 5;
}
</style>
</head>
<body>
<button id="conn">连接服务器</button><br>
<input type="text" id="cmd" placeholder="输入你的命令" />
<br>
<button id="send">发送</button>
<p id="msg">无</p>
</body>
<script type="text/javascript">
var ws=null;
var conn = document.getElementById("conn");
var msg=document.getElementById("msg");
var cmd=document.getElementById("cmd");
var send=document.getElementById("send");
//连接socket
conn.onclick = function() {
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
ws = new WebSocket("ws://127.0.0.1:8080/stm32");
}
else {
alert('当前浏览器 Not support websocket')
}
ws.onopen = function(evt) {
alert("连接成功!")
};
ws.onmessage = function(evt) {
msg.innerText=evt.data;
};
}
send.onclick=function(){
ws.send(cmd.value);
}
</script>
</html>