]> git.scottworley.com Git - reliable-chat/blame - webclient/rc.html
Feed the median-timestamp updates back into the UI
[reliable-chat] / webclient / rc.html
CommitLineData
827f21bb
SW
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
827f21bb 3<html xmlns="http://www.w3.org/1999/xhtml">
520c21fd
SW
4<!--
5 reliable-chat - multipath chat
6 Copyright (C) 2012 Scott Worley <sworley@chkno.net>
7 Copyright (C) 2012 Jason Hibbs <skitch@gmail.com>
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Affero General Public License as
11 published by the Free Software Foundation, either version 3 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Affero General Public License for more details.
18
19 You should have received a copy of the GNU Affero General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21-->
827f21bb
SW
22<head>
23 <title>Reliable Chat</title>
0248a518 24 <style type="text/css"><!--/*--><![CDATA[/*><!--*/
70f46223
JH
25 html, body {
26 width: 99.9%;
27 height: 100%;
28 margin: 0;
29 padding: 0;
b22a2ced
JH
30 background-color: #293134;
31 color: silver;
70f46223
JH
32 font-family: monospace;
33 }
34 #container {
35 height: 100%;
36 }
37 #status {
38 width: 100%;
638866e9 39 text-align: right;
b22a2ced 40 background-color: #293134;
70f46223
JH
41 padding: 5px 5px 5px 0px;
42 }
43 #client {
44 width: 98.5%;
45 padding: 0px 0px 0px 5px;
46 height: 50px;
47 position: fixed;
48 bottom: 0;
49 }
50 #input {
51 width: 100%;
b22a2ced 52 background-color: #293134;
70f46223 53 }
0248a518 54 #say { width: 100% }
70f46223
JH
55 #history {
56 padding: 0px 5px 55px 5px;
57 vertical-align: bottom
58 }
4b184c5f
SW
59 .timestamp {
60 margin-right: 0.8em;
61 }
0248a518
SW
62 img { width: 1px; height: 1px; }
63 iframe { display: none }
70f46223 64 #status span { margin-right: 10px; }
f09c4ede
JH
65 #status span.sad {
66 background-color: #f00;
67 color: #fff;
68 border: 1px solid black;
69 border-radius: 5px;
70 padding-left: 5px;
71 padding-right: 5px;
72 }
73 #status span.happy {
74 background-color: #0f0;
75 color: #000;
76 border: 1px solid black;
77 border-radius: 5px;
78 padding-left: 5px;
79 padding-right: 5px;
80 }
0248a518
SW
81 /*]]>*/--></style>
82 <script type="text/javascript"><!--//--><![CDATA[//><!--
83 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
84
da9ce2ab 85 var session = Math.random(); // For outgoing message IDs
869430fa
SW
86 var since = {}; // server -> time: For fetch?since=
87 var seen = {}; // seen_key -> message
7f54ca0a 88 var history = []; // List of messages sorted by Time
5419ea4c 89 // Messages have these fields:
40380bd3 90 // Time: The timestamp. Median of ServerTimes
5419ea4c
SW
91 // ID: Some unique string for deduping
92 // Text: The text of the message
705e26cf 93 // ServerTimes: server -> timestamp
5419ea4c 94 // UI: The DOM node for this message in the UI
0248a518
SW
95
96 function rcnick() {
97 var nick = localStorage.getItem("nick");
98 if (nick) {
99 return nick;
100 }
101 return 'anonymous';
102 }
103
104 function rcsetnick(new_nick) {
105 localStorage.setItem("nick", new_nick);
106 }
107
108 function rcserverbase(server) {
109 // Add the default port if server doesn't contain a port number already
110 if (server.indexOf(":") == -1) {
111 return "http://" + server + ":21059";
112 } else {
113 return "http://" + server;
114 }
115 }
116
117 function rcchangeserverstatus(server, new_status) {
118 var statusbar = document.getElementById("status");
119 var spans = statusbar.getElementsByTagName("span");
120 for (var i in spans) {
121 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
122 spans[i].setAttribute("class", new_status);
123 }
124 }
125 }
126
bd1ed9dd
SW
127 function rcformattime(t) {
128 var d = t.getDay();
129 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
130 var h = t.getHours();
131 var m = t.getMinutes();
132 var s = t.getSeconds();
133 function pad(x) {
134 return (x < 10 ? "0" : "") + x;
135 }
136 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
137 }
138
0248a518 139 function rcaddmessagetohistory(message) {
7f54ca0a
SW
140 var message_i;
141 if (message.Time) {
142 for (var i = history.length - 1; ; i--) {
143 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
144 message_i = i+1;
145 history.splice(message_i, 0, message);
146 break;
147 }
148 }
149 } else {
150 history.push(message);
151 message_i = history.length-1;
152 }
153
95ba71ee 154 if (!message.UI) {
4b184c5f
SW
155 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
156 var timestamp = document.createElement("span");
157 timestamp.setAttribute("class", "timestamp");
158 timestamp.appendChild(document.createTextNode(timestamp_text));
95ba71ee 159 message.UI = document.createElement("div");
4b184c5f
SW
160 message.UI.appendChild(timestamp);
161 message.UI.appendChild(document.createTextNode(message.Text));
95ba71ee 162 }
0248a518 163 var h = document.getElementById("history");
7f54ca0a
SW
164 if (message_i + 1 < history.length) {
165 h.insertBefore(message.UI, history[message_i + 1].UI);
166 } else {
167 h.appendChild(message.UI);
168 }
0248a518 169 window.scrollTo(0, document.body.scrollHeight);
0248a518
SW
170 }
171
172 function make_seen_key(id, text) {
173 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
174 }
175
40380bd3
SW
176 function rcupdatemessagetime(message) {
177 // Set message.Time to be the median of message.ServerTimes
178 var times = [];
179 for (var i in message.ServerTimes) {
180 times.push(message.ServerTimes[i]);
181 }
182 times.sort();
40380bd3 183 if (times.length % 2) {
ac4bf273 184 message.Time = times[(times.length-1)/2];
40380bd3 185 } else {
ac4bf273 186 var middle = times.length/2;
40380bd3
SW
187 var difference = times[middle].getTime() - times[middle-1].getTime();
188 message.Time = new Date(times[middle-1].getTime() + difference/2);
189 }
95ba71ee
SW
190
191 // This may have broken history's in-sorted-order invariant
192 var hi = history.indexOf(message);
193 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
194 (history[hi+1] && history[hi+1].Time < message.Time)) {
195 history.splice(hi,1);
196 rcaddmessagetohistory(message);
197 }
5be4c8ec
SW
198
199 // Update the UI
200 var spans = message.UI.getElementsByTagName("span");
201 for (var i in spans) {
202 if (spans[i].getAttribute && spans[i].getAttribute("class") == "timestamp") {
203 spans[i].firstChild.data = rcformattime(message.Time);
204 }
205 }
40380bd3
SW
206 }
207
79ced6f1
SW
208 function rcreceivemessages(server, messages) {
209 for (var i in messages) {
ad35b1be 210 var m = messages[i];
bd1ed9dd 211 m.Time = new Date(m.Time);
ad35b1be 212 var seen_key = make_seen_key(m.ID, m.Text);
705e26cf
SW
213 if (seen_key in seen) {
214 seen[seen_key].ServerTimes[server] = m.Time;
40380bd3 215 rcupdatemessagetime(seen[seen_key]);
705e26cf 216 } else {
16640c5d
SW
217 m.ServerTimes = {};
218 m.ServerTimes[server] = m.Time;
869430fa 219 seen[seen_key] = m;
ad35b1be 220 rcaddmessagetohistory(m);
79ced6f1
SW
221 for (var i in servers) {
222 rcchangeserverstatus(servers[i], "sad");
223 }
0248a518 224 }
79ced6f1 225 rcchangeserverstatus(server, "happy");
0248a518 226 }
0248a518
SW
227 }
228
79ced6f1
SW
229 function rcfetch(server) {
230 var delay = 10000; // TODO: Exponential backoff
231 var xhr = new XMLHttpRequest();
232 xhr.onreadystatechange = function() {
233 if (this.readyState == this.DONE) {
234 if (this.status == 200) {
235 var rtxt = this.responseText;
236 if (rtxt != null) {
237 var messages = JSON.parse(rtxt);
238 if (messages != null) {
79ced6f1
SW
239 delay = 40;
240 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
ad35b1be 241 since[server] = messages[messages.length-1].Time;
79ced6f1 242 }
92ca5f8a 243 rcreceivemessages(server, messages);
79ced6f1 244 }
0248a518
SW
245 }
246 }
79ced6f1 247 window.setTimeout(rcfetch, delay, server);
0248a518
SW
248 }
249 }
79ced6f1
SW
250 var uri = rcserverbase(server) + "/fetch";
251 if (server in since) {
252 uri += '?since="' + since[server] + '"';
253 }
254 xhr.open("GET", uri);
255 xhr.send();
0248a518
SW
256 }
257
258 function rcconnect() {
0248a518 259 for (var i in servers) {
79ced6f1 260 rcfetch(servers[i]);
0248a518
SW
261 // Status bar entry
262 var status_indicator = document.createElement("span");
263 status_indicator.appendChild(document.createTextNode(servers[i]));
264 status_indicator.setAttribute("class", "sad");
265 document.getElementById("status").appendChild(status_indicator);
266 }
0248a518
SW
267 }
268
269 function rcsend(d, message) {
7f54ca0a
SW
270 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
271 seen[make_seen_key(message.ID, message.Text)] = message;
0248a518 272 var path = "/speak" +
7f54ca0a
SW
273 "?id=" + encodeURIComponent(message.ID) +
274 "&text=" + encodeURIComponent(message.Text);
0248a518
SW
275 for (var i in servers) {
276 var uri = rcserverbase(servers[i]) + path;
277 var img = document.createElement("img");
278 img.setAttribute("src", uri);
279 d.appendChild(img);
280 }
281 }
282
5a6c082a
SW
283 function rcinput(input) {
284 var message;
285 var re = /^\/([a-z]+) (.*)/
286 var match = re.exec(input);
287 if (match && match[1] == 'me') {
288 message = "* " + rcnick() + " " + match[2];
289 } else if (match && match[1] == 'nick') {
290 message = "*** " + rcnick() + " is now known as " + match[2];
291 rcsetnick(match[2]);
292 } else {
293 message = "<" + rcnick() + "> " + input;
294 }
295
7f54ca0a 296 var m = {'Text': message, 'ServerTimes': {}};
869430fa
SW
297 rcaddmessagetohistory(m);
298 rcsend(m.UI, m);
5a6c082a
SW
299 }
300
0248a518
SW
301 function rckeydown(event) {
302 if (event.keyCode == 13) {
5a6c082a 303 rcinput(document.input.say.value);
0248a518
SW
304 document.input.say.value = "";
305
0248a518
SW
306 }
307 }
308 //--><!]]></script>
309
827f21bb
SW
310</head>
311
f0e385c7 312<body onload="rcconnect()">
70f46223
JH
313 <div id="container">
314 <div id="history"></div>
315 <div id="client">
316 <div id="input">
317 <form name="input" onsubmit="return false" autocomplete="off">
318 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
319 </form></div>
320 <div id="status">&nbsp;</div>
321 </div>
322 </div>
827f21bb
SW
323</body>
324</html>