-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
55 lines (51 loc) · 2.07 KB
/
chat.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
<!DOCTYPE html>
<html>
<head>
<title> Chat Room {{.RoomID}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="static/styles.css">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🛸</text></svg>">
<script>
var ws = new WebSocket("ws://localhost:8080/ws?room={{.RoomID}}");
ws.onmessage = function(event) {
var msg = JSON.parse(event.data);
var chat = document.getElementById("chat");
var line = document.createElement("div");
line.classList.add("message");
var username = document.createElement("span");
username.classList.add("username");
username.textContent = msg.username + ": ";
line.appendChild(username);
var text = document.createElement("span");
text.classList.add("text");
text.textContent = msg.text;
line.appendChild(text);
chat.appendChild(line);
};
function sendMessage() {
var username = document.getElementById("username").value;
var text = document.getElementById("text").value;
var msg = {username: username, text: text};
var xhr = new XMLHttpRequest();
xhr.open("POST", "/message?room={{.RoomID}}");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(msg));
document.getElementById("text").value = "";
}
</script>
</head>
<body>
<h1> Chat Room: {{.RoomID}}</h1>
<div>
<label for="username">Username:</label>
<input type="text" id="username">
</div>
<div>
<label for="text">Message:</label>
<input type="text" id="text">
<button onclick="sendMessage()">Send</button>
</div>
<div id="chat"></div>
</body>
</html>