-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_chat.html
66 lines (60 loc) · 2.55 KB
/
example_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
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html>
<head>
<title>Gametime.js-X chat room</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Parking-Master/Gametime.js-X@latest/example_chat.css">
<script src="https://cdn.jsdelivr.net/gh/Parking-Master/Gametime.js-X@latest/gametime.js"></script>
</head>
<body>
<div id="loading">Loading chat...</div>
<ul id="messages"></ul>
<form id="form">
<input id="input" placeholder="Type your message here...">
<button class="send">Send</button>
<button class="disconnect">Disconnect</button>
</form>
<script>
(async () => {
// We'll try to connect to a Gametime.js-X socket server, but will use PubNub instead if it is not available
await gametime.setCustomServer("https://caribou-needed-implicitly.ngrok-free.app");
await gametime.set("channel", "my-chat-room1234");
await gametime.set("key", "pub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "sub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
await gametime.sustain();
document.querySelector("#loading").remove();
gametime.on("postChatMessage", function(message) {
let item = document.createElement("li");
item.textContent = message;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
let chatHistory = localStorage["history"];
let noBug = chatHistory === "" ? "" : ",";
localStorage.setItem("history", (chatHistory + noBug + encodeURIComponent(message)).split(",").toString());
});
let messages = document.querySelector("#messages");
let form = document.querySelector("#form");
let input = document.querySelector("#input");
if (!localStorage["history"]) {
localStorage.setItem("history", "");
} else {
for (var i = 0; i < localStorage["history"].split(",").length; i++) {
var item = document.createElement("li");
item.textContent = decodeURIComponent(localStorage["history"].split(",")[i]);
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
}
}
form.onsubmit = function(event) {
event.preventDefault();
if (input.value) {
gametime.run("postChatMessage", [input.value]);
input.value = "";
}
};
document.querySelector(".disconnect").onclick = function(event) {
event.preventDefault();
gametime.disconnect();
};
})();
</script>
</body>
</html>