]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 reliable-chat - multipath chat
6 Copyright (C) 2012 Scott Worley <sworley@chkno.net>
7 Copyright (C) 2012 Jason Hibbs <skitch@gmail.com>
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.
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.
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/>.
23 <title>Reliable Chat
</title>
24 <style type=
"text/css"><!--/*--><![CDATA[/*
><!--*/
30 background-color: #293134;
32 font-family: monospace;
40 background-color: #293134;
41 padding: 5px 5px 5px 0px;
45 padding: 0px 0px 0px 5px;
52 background-color: #293134;
56 padding: 0px 5px 55px 5px;
57 vertical-align: bottom
66 img { width: 1px; height: 1px; }
67 iframe { display: none }
68 #status span { margin-right: 10px; }
70 background-color: #f00;
72 border: 1px solid black;
78 background-color: #0f0;
80 border: 1px solid black;
86 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
87 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
89 var session = Math.random(); // For outgoing message IDs
90 var since = {}; // server -> time: For fetch?since=
91 var seen = {}; // seen_key -> message
92 var history = []; // List of messages sorted by Time
93 // Messages have these fields:
94 // Time: The timestamp. Median of ServerTimes
95 // ID: Some unique string for deduping
96 // Text: The text of the message
97 // ServerTimes: server -> timestamp
98 // UI: The DOM node for this message in the UI
101 var nick = localStorage.getItem("nick");
108 function rcsetnick(new_nick) {
109 localStorage.setItem("nick", new_nick);
112 function rcserverbase(server) {
113 // Add the default port if server doesn't contain a port number already
114 if (server.indexOf(":") == -1) {
115 return "http://" + server + ":21059";
117 return "http://" + server;
121 function rcchangeserverstatus(server, new_status) {
122 var statusbar = document.getElementById("status");
123 var spans = statusbar.getElementsByTagName("span");
124 for (var i in spans) {
125 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
126 spans[i].setAttribute("class", new_status);
131 function rcformattime(t) {
133 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
134 var h = t.getHours();
135 var m = t.getMinutes();
136 var s = t.getSeconds();
138 return (x < 10 ? "0" : "") + x;
140 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
143 function rcmakemessageUI(message) {
144 message.UI = document.createElement("div");
147 var servercount = document.createElement("span");
148 servercount.setAttribute("class", "servercount");
149 servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length));
150 message.UI.appendChild(servercount);
153 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
154 var timestamp = document.createElement("span");
155 timestamp.setAttribute("class", "timestamp");
156 timestamp.appendChild(document.createTextNode(timestamp_text));
157 message.UI.appendChild(timestamp);
159 // Classify different message types
160 var text_span = document.createElement("span");
162 if (/^\*\*\* /.test(message.Text)) {
164 } else if (/^\* /.test(message.Text)) {
169 if (Object.keys(message.ServerTimes).length == 0) {
172 text_span.setAttribute("class", type);
173 text_span.appendChild(document.createTextNode(message.Text));
174 message.UI.appendChild(text_span);
177 function rcaddmessagetohistory(message) {
180 for (var i = history.length - 1; ; i--) {
181 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
183 history.splice(message_i, 0, message);
188 history.push(message);
189 message_i = history.length-1;
193 rcmakemessageUI(message);
195 var h = document.getElementById("history");
196 if (message_i + 1 < history.length) {
197 h.insertBefore(message.UI, history[message_i + 1].UI);
199 h.appendChild(message.UI);
201 window.scrollTo(0, document.body.scrollHeight);
204 function make_seen_key(id, text) {
205 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
208 function rcupdatemessagetime(message) {
209 // Set message.Time to be the median of message.ServerTimes
211 for (var i in message.ServerTimes) {
212 times.push(message.ServerTimes[i]);
215 if (times.length % 2) {
216 message.Time = times[(times.length-1)/2];
218 var middle = times.length/2;
219 var difference = times[middle].getTime() - times[middle-1].getTime();
220 message.Time = new Date(times[middle-1].getTime() + difference/2);
223 // This may have broken history's in-sorted-order invariant
224 var hi = history.indexOf(message);
225 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
226 (history[hi+1] && history[hi+1].Time < message.Time)) {
227 history.splice(hi,1);
228 rcaddmessagetohistory(message);
232 var spans = message.UI.getElementsByTagName("span");
233 for (var i in spans) {
234 if (spans[i].getAttribute) {
235 var type = spans[i].getAttribute("class");
236 if (type == "servercount") {
237 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
238 } else if (type == "timestamp") {
239 spans[i].firstChild.data = rcformattime(message.Time);
245 function rcreceivemessages(server, messages) {
246 for (var i in messages) {
248 m.Time = new Date(m.Time);
249 var seen_key = make_seen_key(m.ID, m.Text);
250 if (seen_key in seen) {
251 seen[seen_key].ServerTimes[server] = m.Time;
252 rcupdatemessagetime(seen[seen_key]);
255 m.ServerTimes[server] = m.Time;
257 rcaddmessagetohistory(m);
258 for (var i in servers) {
259 rcchangeserverstatus(servers[i], "sad");
262 rcchangeserverstatus(server, "happy");
266 function rcfetch(server) {
267 var delay = 10000; // TODO: Exponential backoff
268 var xhr = new XMLHttpRequest();
269 xhr.onreadystatechange = function() {
270 if (this.readyState == this.DONE) {
271 if (this.status == 200) {
272 var rtxt = this.responseText;
274 var messages = JSON.parse(rtxt);
275 if (messages != null) {
277 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
278 since[server] = messages[messages.length-1].Time;
280 rcreceivemessages(server, messages);
284 window.setTimeout(rcfetch, delay, server);
287 var uri = rcserverbase(server) + "/fetch";
288 if (server in since) {
289 uri += '?since="' + since[server] + '"';
291 xhr.open("GET", uri);
295 function rcconnect() {
296 for (var i in servers) {
299 var status_indicator = document.createElement("span");
300 status_indicator.appendChild(document.createTextNode(servers[i]));
301 status_indicator.setAttribute("class", "sad");
302 document.getElementById("status").appendChild(status_indicator);
306 function rcsend(d, message) {
307 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
308 seen[make_seen_key(message.ID, message.Text)] = message;
309 var path = "/speak" +
310 "?id=" + encodeURIComponent(message.ID) +
311 "&text=" + encodeURIComponent(message.Text);
312 for (var i in servers) {
313 var uri = rcserverbase(servers[i]) + path;
314 var img = document.createElement("img");
315 img.setAttribute("src", uri);
320 function rcinput(input) {
322 var re = /^\/([a-z]+) (.*)/
323 var match = re.exec(input);
324 if (match && match[1] == 'me') {
325 message = "* " + rcnick() + " " + match[2];
326 } else if (match && match[1] == 'nick') {
327 message = "*** " + rcnick() + " is now known as " + match[2];
330 message = "<" + rcnick() + "> " + input;
333 var m = {'Text': message, 'ServerTimes': {}};
334 rcaddmessagetohistory(m);
338 function rckeydown(event) {
339 if (event.keyCode == 13) {
340 rcinput(document.input.say.value);
341 document.input.say.value = "";
349 <body onload=
"rcconnect()">
351 <div id=
"history"></div>
354 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
355 <input id=
"say" onkeydown=
"return rckeydown(event)" autocomplete=
"off" autofocus=
"autofocus"></input>
357 <div id=
"status"> </div>