]>
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
62 img { width: 1px; height: 1px; }
63 iframe { display: none }
64 #status span { margin-right: 10px; }
66 background-color: #f00;
68 border: 1px solid black;
74 background-color: #0f0;
76 border: 1px solid black;
82 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
83 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
85 var session = Math.random(); // For outgoing message IDs
86 var since = {}; // server -> time: For fetch?since=
87 var seen = {}; // seen_key -> message
88 var history = []; // List of messages sorted by Time
89 // Messages have these fields:
90 // Time: The timestamp. Median of ServerTimes
91 // ID: Some unique string for deduping
92 // Text: The text of the message
93 // ServerTimes: server -> timestamp
94 // UI: The DOM node for this message in the UI
97 var nick = localStorage.getItem("nick");
104 function rcsetnick(new_nick) {
105 localStorage.setItem("nick", new_nick);
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";
113 return "http://" + server;
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);
127 function rcformattime(t) {
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();
134 return (x < 10 ? "0" : "") + x;
136 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
139 function rcaddmessagetohistory(message) {
142 for (var i = history.length - 1; ; i--) {
143 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
145 history.splice(message_i, 0, message);
150 history.push(message);
151 message_i = history.length-1;
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));
159 message.UI = document.createElement("div");
160 message.UI.appendChild(timestamp);
161 message.UI.appendChild(document.createTextNode(message.Text));
163 var h = document.getElementById("history");
164 if (message_i + 1 < history.length) {
165 h.insertBefore(message.UI, history[message_i + 1].UI);
167 h.appendChild(message.UI);
169 window.scrollTo(0, document.body.scrollHeight);
172 function make_seen_key(id, text) {
173 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
176 function rcupdatemessagetime(message) {
177 // Set message.Time to be the median of message.ServerTimes
179 for (var i in message.ServerTimes) {
180 times.push(message.ServerTimes[i]);
183 if (times.length % 2) {
184 message.Time = times[(times.length-1)/2];
186 var middle = times.length/2;
187 var difference = times[middle].getTime() - times[middle-1].getTime();
188 message.Time = new Date(times[middle-1].getTime() + difference/2);
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);
200 function rcreceivemessages(server, messages) {
201 for (var i in messages) {
203 m.Time = new Date(m.Time);
204 var seen_key = make_seen_key(m.ID, m.Text);
205 if (seen_key in seen) {
206 seen[seen_key].ServerTimes[server] = m.Time;
207 rcupdatemessagetime(seen[seen_key]);
210 m.ServerTimes[server] = m.Time;
212 rcaddmessagetohistory(m);
213 for (var i in servers) {
214 rcchangeserverstatus(servers[i], "sad");
217 rcchangeserverstatus(server, "happy");
221 function rcfetch(server) {
222 var delay = 10000; // TODO: Exponential backoff
223 var xhr = new XMLHttpRequest();
224 xhr.onreadystatechange = function() {
225 if (this.readyState == this.DONE) {
226 if (this.status == 200) {
227 var rtxt = this.responseText;
229 var messages = JSON.parse(rtxt);
230 if (messages != null) {
232 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
233 since[server] = messages[messages.length-1].Time;
235 rcreceivemessages(server, messages);
239 window.setTimeout(rcfetch, delay, server);
242 var uri = rcserverbase(server) + "/fetch";
243 if (server in since) {
244 uri += '?since="' + since[server] + '"';
246 xhr.open("GET", uri);
250 function rcconnect() {
251 for (var i in servers) {
254 var status_indicator = document.createElement("span");
255 status_indicator.appendChild(document.createTextNode(servers[i]));
256 status_indicator.setAttribute("class", "sad");
257 document.getElementById("status").appendChild(status_indicator);
261 function rcsend(d, message) {
262 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
263 seen[make_seen_key(message.ID, message.Text)] = message;
264 var path = "/speak" +
265 "?id=" + encodeURIComponent(message.ID) +
266 "&text=" + encodeURIComponent(message.Text);
267 for (var i in servers) {
268 var uri = rcserverbase(servers[i]) + path;
269 var img = document.createElement("img");
270 img.setAttribute("src", uri);
275 function rcinput(input) {
277 var re = /^\/([a-z]+) (.*)/
278 var match = re.exec(input);
279 if (match && match[1] == 'me') {
280 message = "* " + rcnick() + " " + match[2];
281 } else if (match && match[1] == 'nick') {
282 message = "*** " + rcnick() + " is now known as " + match[2];
285 message = "<" + rcnick() + "> " + input;
288 var m = {'Text': message, 'ServerTimes': {}};
289 rcaddmessagetohistory(m);
293 function rckeydown(event) {
294 if (event.keyCode == 13) {
295 rcinput(document.input.say.value);
296 document.input.say.value = "";
304 <body onload=
"rcconnect()">
306 <div id=
"history"></div>
309 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
310 <input id=
"say" onkeydown=
"return rckeydown(event)" autocomplete=
"off" autofocus=
"autofocus"></input>
312 <div id=
"status"> </div>