-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.html
120 lines (120 loc) · 3.35 KB
/
home.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!doctype html>
<html lang="en">
<head>
<title>Silence</title>
<script type="text/javascript">
window.onload = function () {
var conn
var msg = document.getElementById("msg")
var log = document.getElementById("log")
function appendLog(item) {
var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1
log.appendChild(item)
if (doScroll) {
log.scrollTop = log.scrollHeight - log.clientHeight
}
}
document.getElementById("form").onsubmit = function () {
if (!conn) {
return false
}
if (!msg.value) {
return false
}
conn.send(msg.value)
msg.value = ""
return false
}
if (window["WebSocket"]) {
var wsPath = "/ws"
var base =
location.hostname + (location.port ? ":" + location.port : "")
if (location.protocol === "https:") {
wsPath = "wss://" + base + wsPath
} else if (location.protocol === "http:") {
wsPath = "ws://" + base + wsPath
}
conn = new WebSocket(wsPath)
conn.onclose = function (evt) {
var item = document.createElement("div")
item.innerHTML = "<b>Connection closed.</b>"
appendLog(item)
}
conn.onmessage = function (evt) {
var messages = evt.data.split("\n")
for (var i = 0; i < messages.length; i++) {
var s = messages[i]
const id = s.split(" ", 1)[0]
var msg = ""
var is = s.indexOf(" ")
if (is > 0) {
msg = s.slice(is + 1)
}
var idElem = document.createElement("span")
idElem.innerText = id
idElem.style.color = "#" + id
idElem.id = "id"
var msgElem = document.createElement("span")
msgElem.innerText = msg
var item = document.createElement("div")
item.appendChild(idElem)
item.appendChild(msgElem)
appendLog(item)
}
}
} else {
var item = document.createElement("div")
item.innerHTML = "<b>Your browser does not support WebSockets.</b>"
appendLog(item)
}
}
</script>
<style type="text/css">
html {
background-color: #1e1e1e;
color: #fefefe;
}
*:focus {
outline: none;
box-shadow: none;
}
#log {
margin: 0;
padding: 0.5em 0.5em 0.5em 0.5em;
position: absolute;
top: 0.5em;
left: 0.5em;
right: 0.5em;
bottom: 3em;
overflow: auto;
font-family: monospace;
}
#form {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
overflow: hidden;
display: flex;
}
#msg {
display: flex;
flex-grow: 1;
}
#send {
padding: 5px;
border-radius: 0;
}
#id {
padding-right: 0.5rem;
}
</style>
</head>
<body>
<div id="log"></div>
<form id="form">
<input type="text" id="msg" placeholder="Message" autofocus />
<input type="submit" id="send" value="Send" />
</form>
</body>
</html>