-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
154 lines (143 loc) · 4.3 KB
/
index.html
File metadata and controls
154 lines (143 loc) · 4.3 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Page Properties -->
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1,
shrink-to-fit=no"
/>
<!-- Site Title and Description -->
<title>WebSocket Service</title>
<!-- Scripts and Styling -->
<script type="application/javascript" async defer>
/* JavaScript! */
var update_number = 1;
/**
* Checks if being served from localhost and returns proper backend URL.
* @returns {string} The URL for the backend websocket connection.
*/
function getServerUrl() {
var url = window.location.href.toString();
// TODO: Replace this with deployment URL.
var server = "wss://" + location.host;
// If running in development, point requests to localhost:8000
if (url.indexOf("localhost") == 7) {
console.log(
"getServerUrl: In development mode, using local backend."
);
server = "ws://localhost:3000";
}
console.log("Using server " + server);
return server;
}
function addText(string, elementId) {
var bb = document.getElementById(elementId);
var msg = document.createElement("p");
msg.innerText = string;
bb.appendChild(msg);
msg.scrollIntoView();
}
function onClose(event) {
console.log("disconnected");
}
function setupBellboy() {
// Creates a websocket connection as the bellboy.
let bellboy = new WebSocket(getServerUrl());
bellboy.onopen = function () {
console.log("connection opened");
bellboy.send("BELLBOY"); // Register.
setInterval(function () {
console.log(
"Sending hello #" +
update_number +
" to the backend from the bellboy..."
);
var str = "Status update #" + update_number++ + " from bellboy.";
bellboy.send(str);
str = "SEND: " + str;
addText(str, "bellboy-text");
}, 1000);
};
}
/**
* To be implemented in Website repository:
* "Live feed" with messages from all active bellboys.
*
* Create a scrolling box that, when onmessage is called and
* a new message is received, adds the message to the bottom of the
* scroll-box. Can be implemented based on the minimal
* example below.
*
*/
function setupWebsite() {
// Creates a websocket connection as the website.
let website = new WebSocket(getServerUrl());
website.onopen = function () {
website.send("WEBSITE"); // Register.
website.onmessage = function (event) {
console.log("Got data: " + event.data);
var str = "RECV: " + event.data;
addText(str, "website-text");
};
};
}
/*
* On load operation. Calls a number of functions once the DOM is ready.
* All features should be called in this block.
*/
window.addEventListener("load", function (event) {
setupBellboy();
setupWebsite();
onClose();
});
</script>
<style>
html,
body {
background: white;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
height: 98%;
overflow: hidden;
text-align: left;
}
.container {
display: flex;
height: 98%;
}
.child {
flex: 1;
border: 1px solid black;
padding: 5px;
margin: 5px;
height: 98%;
display: block;
}
.scroll-container {
border: 1px solid black;
height: 80%;
overflow-y: scroll;
}
</style>
</head>
<body>
<div class="container">
<div class="child">
<h1>Bellboy</h1>
<p>Log messages:</p>
<div class="scroll-container">
<div id="bellboy-text" class="scroll"></div>
</div>
</div>
<div class="child">
<h1>Website</h1>
<p>Log messages:</p>
<div class="scroll-container">
<div id="website-text" class="scroll"></div>
</div>
</div>
</div>
</body>
</html>