]>
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
59 img { width: 1px; height: 1px; }
60 iframe { display: none }
61 #status span { margin-right: 10px; }
63 background-color: #f00;
65 border: 1px solid black;
71 background-color: #0f0;
73 border: 1px solid black;
79 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
80 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
82 var session = Math.random(); // For outgoing message IDs
83 var since = {}; // server -> time: For fetch?since=
84 var seen = {}; // seen_key -> true
87 var nick = localStorage.getItem("nick");
94 function rcsetnick(new_nick) {
95 localStorage.setItem("nick", new_nick);
98 function rcserverbase(server) {
99 // Add the default port if server doesn't contain a port number already
100 if (server.indexOf(":") == -1) {
101 return "http://" + server + ":21059";
103 return "http://" + server;
107 function rcchangeserverstatus(server, new_status) {
108 var statusbar = document.getElementById("status");
109 var spans = statusbar.getElementsByTagName("span");
110 for (var i in spans) {
111 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
112 spans[i].setAttribute("class", new_status);
117 function rcaddmessagetohistory(message) {
118 var d = document.createElement("div");
119 d.appendChild(document.createTextNode(message.Text));
120 var h = document.getElementById("history");
122 window.scrollTo(0, document.body.scrollHeight);
126 function make_seen_key(id, text) {
127 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
130 function rcreceivemessages(server, messages) {
131 for (var i in messages) {
133 var seen_key = make_seen_key(m.ID, m.Text);
134 if (!(seen_key in seen)) {
135 seen[seen_key] = true;
136 rcaddmessagetohistory(m);
137 for (var i in servers) {
138 rcchangeserverstatus(servers[i], "sad");
141 rcchangeserverstatus(server, "happy");
145 function rcfetch(server) {
146 var delay = 10000; // TODO: Exponential backoff
147 var xhr = new XMLHttpRequest();
148 xhr.onreadystatechange = function() {
149 if (this.readyState == this.DONE) {
150 if (this.status == 200) {
151 var rtxt = this.responseText;
153 var messages = JSON.parse(rtxt);
154 if (messages != null) {
155 rcreceivemessages(server, messages);
157 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
158 since[server] = messages[messages.length-1].Time;
163 window.setTimeout(rcfetch, delay, server);
166 var uri = rcserverbase(server) + "/fetch";
167 if (server in since) {
168 uri += '?since="' + since[server] + '"';
170 xhr.open("GET", uri);
174 function rcconnect() {
175 for (var i in servers) {
178 var status_indicator = document.createElement("span");
179 status_indicator.appendChild(document.createTextNode(servers[i]));
180 status_indicator.setAttribute("class", "sad");
181 document.getElementById("status").appendChild(status_indicator);
185 function rcsend(d, message) {
186 var id = new Date().getTime() + "-" + session + "-" + Math.random();
187 seen[make_seen_key(id, message)] = true;
188 var path = "/speak" +
189 "?id=" + encodeURIComponent(id) +
190 "&text=" + encodeURIComponent(message);
191 for (var i in servers) {
192 var uri = rcserverbase(servers[i]) + path;
193 var img = document.createElement("img");
194 img.setAttribute("src", uri);
199 function rcinput(input) {
201 var re = /^\/([a-z]+) (.*)/
202 var match = re.exec(input);
203 if (match && match[1] == 'me') {
204 message = "* " + rcnick() + " " + match[2];
205 } else if (match && match[1] == 'nick') {
206 message = "*** " + rcnick() + " is now known as " + match[2];
209 message = "<" + rcnick() + "> " + input;
212 var d = rcaddmessagetohistory({'Text': message});
216 function rckeydown(event) {
217 if (event.keyCode == 13) {
218 rcinput(document.input.say.value);
219 document.input.say.value = "";
227 <body onload=
"rcconnect()">
229 <div id=
"history"></div>
232 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
233 <input id=
"say" onkeydown=
"return rckeydown(event)" autocomplete=
"off" autofocus=
"autofocus"></input>
235 <div id=
"status"> </div>