-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
66 lines (58 loc) · 2.31 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
var ws;
function createChatEntry(username, message) {
var entry = document.createElement("div");
entry.class = "chat_entry";
var dom_uname = document.createElement("span");
dom_uname.class = "chat_username";
dom_uname.innerHTML = username+": ";
entry.appendChild(dom_uname);
var dom_msg = document.createElement("span");
dom_msg.class = "chat_message";
dom_msg.innerHTML = message;
entry.appendChild(dom_msg);
return entry;
}
function openWS(messageContainer) {
ws = new WebSocket("ws://localhost:8042/control");
ws.onmessage = function(e) {
var data = JSON.parse(e.data);
messageContainer.appendChild(createChatEntry(data.author, data.message));
};
ws.onclose = function(e) {
openWS(messageContainer);
};
}
function sendMessage() {
var data = { author: document.getElementById("username").value,
message: document.getElementById("message").value };
if(data.author && data.message) {
ws.send(JSON.stringify(data));
}
}
window.onload = function() {
var messageContainer = document.getElementById("chat");
if("WebSocket" in window) {
messageContainer.appendChild(createChatEntry("[SYSTEM]", "WebSocket is supported by your browser!"));
messageContainer.appendChild(createChatEntry("[SYSTEM]", "Pick a username and dropping some beats."));
openWS(messageContainer);
}
else {
messageContainer.appendChild(createChatEntry("[SYSTEM]", "WebSocket is NOT supported by your browser!"));
}
}
</script>
</head>
<body>
<div id="chat" style="width: 100%; height: 40em; overflow: scroll; font-family: Arial"></div>
<div id="input_area">
<input id="username" type="text" placeholder="Your username" style="display: block; width: 200px"></input>
<textarea id="message" placeholder="Your message" style="display: block; width: 400px"></textarea>
<button onclick="sendMessage()" style="display: block">Send</button>
</div>
</body>
</html>