-
Notifications
You must be signed in to change notification settings - Fork 28
/
2013-12-19-client.html
43 lines (40 loc) · 1.25 KB
/
2013-12-19-client.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
<!DOCTYPE html>
<html>
<head>
<title>Tell Santa What You Want</title>
<style>
.me { font-style: italic; background: #eee; }
ul li { display: block; width: 100%; list-style-type: none; margin: 3px; border: 1px solid #ccc; };
</style>
</head>
<body>
<p>Tell Santa what you want for Christmas:</p>
<ul id="repl">
</ul>
<input id="prompt" disabled="disabled" />
<script>
var prompt = document.getElementById('prompt');
var connectionToSanta = new WebSocket("ws://localhost:8080/");
var messages = document.getElementById('repl');
function log(message, cls) {
var messageNode = document.createElement("li");
messageNode.innerHTML = message;
messageNode.className = cls;
repl.appendChild(messageNode);
}
connectionToSanta.onopen = function(e) {
prompt.disabled = false;
prompt.onkeydown = function (e) {
if (e.keyCode == 13) {
connectionToSanta.send(prompt.value);
log(prompt.value, "me");
prompt.value = "";
}
}
};
connectionToSanta.onmessage = function (m) {
log(m.data, "");
}
</script>
</body>
</html>