47 lines
1.5 KiB
HTML
47 lines
1.5 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
|
||
<head>
|
||
<meta charset='UTF-8'>
|
||
<title>高温预警器</title>
|
||
<script>
|
||
var ws;
|
||
var temp = 28;
|
||
window.onload = function () {
|
||
if ('WebSocket' in window) {
|
||
ws = new WebSocket('ws://' + window.location.host + '/');
|
||
ws.onopen = function () {
|
||
document.getElementById('info').innerHTML +=
|
||
'WebSocket连接成功!' + '<br>';
|
||
ws.send('connect ok!');
|
||
};
|
||
ws.onmessage = function (evt) {
|
||
document.getElementById('temp').innerHTML =
|
||
'当前温度:' + evt.data + '℃';
|
||
};
|
||
ws.onerror = function () {
|
||
document.getElementById('info').innerHTML +=
|
||
'通讯发送错误!' + '<br>';
|
||
};
|
||
ws.onclose = function () {
|
||
document.getElementById('info').innerHTML +=
|
||
'WebSocketTest连接已关闭!' + '<br>';
|
||
};
|
||
} else {
|
||
document.getElementById('info').innerHTML =
|
||
'浏览器不支持 WebSocket!';
|
||
}
|
||
};
|
||
</script>
|
||
</head>
|
||
|
||
<body>
|
||
<h1 id='temp'>当前温度:27.00℃</h1>
|
||
<form action='temp' method='post'>
|
||
<input name='temp' id='tempi' />
|
||
<button name='submit' value='submit'>设置温度</button>
|
||
</form>
|
||
<div id=' info'></div>
|
||
</body>
|
||
|
||
</html> |