]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
f3abaf3faadee799136099b6eaf9b685695db784
[reliable-chat] / 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">
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 -->
22 <head>
23 <title>Reliable Chat</title>
24 <style type="text/css"><!--/*--><![CDATA[/*><!--*/
25 html, body {
26 width: 99.9%;
27 height: 100%;
28 margin: 0;
29 padding: 0;
30 background-color: #293134;
31 color: silver;
32 font-family: monospace;
33 }
34 #container {
35 height: 100%;
36 }
37 #status {
38 width: 100%;
39 text-align: right;
40 background-color: #293134;
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%;
52 background-color: #293134;
53 }
54 #say { width: 100% }
55 #history {
56 padding: 0px 5px 55px 5px;
57 vertical-align: bottom
58 }
59 img { width: 1px; height: 1px; }
60 iframe { display: none }
61 #status span { margin-right: 10px; }
62 #status span.sad {
63 background-color: #f00;
64 color: #fff;
65 border: 1px solid black;
66 border-radius: 5px;
67 padding-left: 5px;
68 padding-right: 5px;
69 }
70 #status span.happy {
71 background-color: #0f0;
72 color: #000;
73 border: 1px solid black;
74 border-radius: 5px;
75 padding-left: 5px;
76 padding-right: 5px;
77 }
78 /*]]>*/--></style>
79 <script type="text/javascript"><!--//--><![CDATA[//><!--
80 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
81
82 var session = Math.random(); // For outgoing message IDs
83 var since = {}; // server -> time: For fetch?since=
84 var seen = {}; // seen_key -> message
85 // Messages have these fields:
86 // Time: The timestamp. Median of ServerTimes
87 // ID: Some unique string for deduping
88 // Text: The text of the message
89 // ServerTimes: server -> timestamp
90 // UI: The DOM node for this message in the UI
91
92 function rcnick() {
93 var nick = localStorage.getItem("nick");
94 if (nick) {
95 return nick;
96 }
97 return 'anonymous';
98 }
99
100 function rcsetnick(new_nick) {
101 localStorage.setItem("nick", new_nick);
102 }
103
104 function rcserverbase(server) {
105 // Add the default port if server doesn't contain a port number already
106 if (server.indexOf(":") == -1) {
107 return "http://" + server + ":21059";
108 } else {
109 return "http://" + server;
110 }
111 }
112
113 function rcchangeserverstatus(server, new_status) {
114 var statusbar = document.getElementById("status");
115 var spans = statusbar.getElementsByTagName("span");
116 for (var i in spans) {
117 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
118 spans[i].setAttribute("class", new_status);
119 }
120 }
121 }
122
123 function rcformattime(t) {
124 var d = t.getDay();
125 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
126 var h = t.getHours();
127 var m = t.getMinutes();
128 var s = t.getSeconds();
129 function pad(x) {
130 return (x < 10 ? "0" : "") + x;
131 }
132 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
133 }
134
135 function rcaddmessagetohistory(message) {
136 message.UI = document.createElement("div");
137 var text = (message.Time ? rcformattime(message.Time) : "") + " " + message.Text;
138 message.UI.appendChild(document.createTextNode(text));
139 var h = document.getElementById("history");
140 h.appendChild(message.UI);
141 window.scrollTo(0, document.body.scrollHeight);
142 }
143
144 function make_seen_key(id, text) {
145 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
146 }
147
148 function rcupdatemessagetime(message) {
149 // Set message.Time to be the median of message.ServerTimes
150 var times = [];
151 for (var i in message.ServerTimes) {
152 times.push(message.ServerTimes[i]);
153 }
154 times.sort();
155 var middle = times.length/2;
156 if (times.length % 2) {
157 message.Time = times[middle];
158 } else {
159 var difference = times[middle].getTime() - times[middle-1].getTime();
160 message.Time = new Date(times[middle-1].getTime() + difference/2);
161 }
162 }
163
164 function rcreceivemessages(server, messages) {
165 for (var i in messages) {
166 var m = messages[i];
167 m.Time = new Date(m.Time);
168 var seen_key = make_seen_key(m.ID, m.Text);
169 if (seen_key in seen) {
170 seen[seen_key].ServerTimes[server] = m.Time;
171 rcupdatemessagetime(seen[seen_key]);
172 } else {
173 m.ServerTimes = {};
174 m.ServerTimes[server] = m.Time;
175 seen[seen_key] = m;
176 rcaddmessagetohistory(m);
177 for (var i in servers) {
178 rcchangeserverstatus(servers[i], "sad");
179 }
180 }
181 rcchangeserverstatus(server, "happy");
182 }
183 }
184
185 function rcfetch(server) {
186 var delay = 10000; // TODO: Exponential backoff
187 var xhr = new XMLHttpRequest();
188 xhr.onreadystatechange = function() {
189 if (this.readyState == this.DONE) {
190 if (this.status == 200) {
191 var rtxt = this.responseText;
192 if (rtxt != null) {
193 var messages = JSON.parse(rtxt);
194 if (messages != null) {
195 delay = 40;
196 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
197 since[server] = messages[messages.length-1].Time;
198 }
199 rcreceivemessages(server, messages);
200 }
201 }
202 }
203 window.setTimeout(rcfetch, delay, server);
204 }
205 }
206 var uri = rcserverbase(server) + "/fetch";
207 if (server in since) {
208 uri += '?since="' + since[server] + '"';
209 }
210 xhr.open("GET", uri);
211 xhr.send();
212 }
213
214 function rcconnect() {
215 for (var i in servers) {
216 rcfetch(servers[i]);
217 // Status bar entry
218 var status_indicator = document.createElement("span");
219 status_indicator.appendChild(document.createTextNode(servers[i]));
220 status_indicator.setAttribute("class", "sad");
221 document.getElementById("status").appendChild(status_indicator);
222 }
223 }
224
225 function rcsend(d, message) {
226 var id = new Date().getTime() + "-" + session + "-" + Math.random();
227 seen[make_seen_key(id, message)] = message;
228 var path = "/speak" +
229 "?id=" + encodeURIComponent(id) +
230 "&text=" + encodeURIComponent(message);
231 for (var i in servers) {
232 var uri = rcserverbase(servers[i]) + path;
233 var img = document.createElement("img");
234 img.setAttribute("src", uri);
235 d.appendChild(img);
236 }
237 }
238
239 function rcinput(input) {
240 var message;
241 var re = /^\/([a-z]+) (.*)/
242 var match = re.exec(input);
243 if (match && match[1] == 'me') {
244 message = "* " + rcnick() + " " + match[2];
245 } else if (match && match[1] == 'nick') {
246 message = "*** " + rcnick() + " is now known as " + match[2];
247 rcsetnick(match[2]);
248 } else {
249 message = "<" + rcnick() + "> " + input;
250 }
251
252 var m = {'Text': message};
253 rcaddmessagetohistory(m);
254 rcsend(m.UI, m);
255 }
256
257 function rckeydown(event) {
258 if (event.keyCode == 13) {
259 rcinput(document.input.say.value);
260 document.input.say.value = "";
261
262 }
263 }
264 //--><!]]></script>
265
266 </head>
267
268 <body onload="rcconnect()">
269 <div id="container">
270 <div id="history"></div>
271 <div id="client">
272 <div id="input">
273 <form name="input" onsubmit="return false" autocomplete="off">
274 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
275 </form></div>
276 <div id="status">&nbsp;</div>
277 </div>
278 </div>
279 </body>
280 </html>