-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpage.go
83 lines (68 loc) · 1.88 KB
/
webpage.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
const CmdSocketHTMLPage = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cmd Socket</title>
<style>
.stick-head{
display: flex;
position: sticky;
top: 0px;
background-color: white;
}
.stick-foot{
display: flex;
position: sticky;
bottom: 0px;
background-color: white;
}
</style>
</head>
<body>
<h3 class="stick-head">Cmd Socket Go</h3>
<pre id="output"></pre>
<div class="stick-foot">
<input id="cmd">
<button id="sendcmd" onclick="sendmsg()">send</button>
<button id="clearcmd" onclick="clearmsg()">clear</button>
</div>
<script>
url = "ws://"+window.location.host+{{.Socket_Path}}
c = new WebSocket(url);
function sendmsg(){
console.log("click");
div = document.getElementById("cmd");
c.send(div.value);
}
function clearmsg(){
let div = document.getElementById("output");
if (div != null) div.innerHTML="";
}
c.onmessage = function(msg){
document.getElementById("output").append((new Date())+ " <== "+msg.data+"\n");
window.scrollTo(0,document.body.scrollHeight);
console.log(msg);
}
c.onopen = function(evt){
document.getElementById("output").append("WebSocket Open : \n");
console.log("open : ",evt);
}
c.onclose = function(evt){
document.getElementById("output").append("WebSocket Closed !! \n");
console.log("close : ",evt);
}
c.onerror = function (evt) {
document.getElementById("output").append("WebSocket Get Error : "+evt+"\n");
console.log(evt);
}
document.getElementById("cmd").addEventListener("keyup", function (e) {
if (e.key === 'Enter' || e.keyCode === 13) {
sendmsg();
}
})
</script>
</body>
</html>
`