]>
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">
4 <html xmlns=
"http://www.w3.org/1999/xhtml">
6 <title>Reliable Chat
</title>
7 <style type=
"text/css"><!--/*--><![CDATA[/*
><!--*/
13 background-color: #293134;
15 font-family: monospace;
23 background-color: #293134;
24 padding: 5px 5px 5px 0px;
28 padding: 0px 0px 0px 5px;
35 background-color: #293134;
39 padding: 0px 5px 55px 5px;
40 vertical-align: bottom
42 img { width: 1px; height: 1px; }
43 iframe { display: none }
44 #status span { margin-right: 10px; }
46 background-color: #f00;
48 border: 1px solid black;
54 background-color: #0f0;
56 border: 1px solid black;
62 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
63 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
65 var session = Math.random();
70 var nick = localStorage.getItem("nick");
77 function rcsetnick(new_nick) {
78 localStorage.setItem("nick", new_nick);
81 function rcserverbase(server) {
82 // Add the default port if server doesn't contain a port number already
83 if (server.indexOf(":") == -1) {
84 return "http://" + server + ":21059";
86 return "http://" + server;
90 function rcchangeserverstatus(server, new_status) {
91 var statusbar = document.getElementById("status");
92 var spans = statusbar.getElementsByTagName("span");
93 for (var i in spans) {
94 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
95 spans[i].setAttribute("class", new_status);
100 function rcaddmessagetohistory(message) {
101 var d = document.createElement("div");
102 d.appendChild(document.createTextNode(message));
103 var h = document.getElementById("history");
105 window.scrollTo(0, document.body.scrollHeight);
109 function make_seen_key(id, text) {
110 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
113 function rcreceivemessages(server, messages) {
114 for (var i in messages) {
115 var seen_key = make_seen_key(messages[i]['ID'], messages[i]['Text']);
116 if (!(seen_key in seen)) {
117 seen[seen_key] = true;
118 rcaddmessagetohistory(messages[i]['Text']);
119 for (var i in servers) {
120 rcchangeserverstatus(servers[i], "sad");
123 rcchangeserverstatus(server, "happy");
127 function rcfetch(server) {
128 var delay = 10000; // TODO: Exponential backoff
129 var xhr = new XMLHttpRequest();
130 xhr.onreadystatechange = function() {
131 if (this.readyState == this.DONE) {
132 if (this.status == 200) {
133 var rtxt = this.responseText;
135 var messages = JSON.parse(rtxt);
136 if (messages != null) {
137 rcreceivemessages(server, messages);
138 window.parent.postMessage(rtxt, "*");
140 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
141 since[server] = messages[messages.length-1]["Time"];
146 window.setTimeout(rcfetch, delay, server);
149 var uri = rcserverbase(server) + "/fetch";
150 if (server in since) {
151 uri += '?since="' + since[server] + '"';
153 xhr.open("GET", uri);
157 function rcconnect() {
158 for (var i in servers) {
161 var status_indicator = document.createElement("span");
162 status_indicator.appendChild(document.createTextNode(servers[i]));
163 status_indicator.setAttribute("class", "sad");
164 document.getElementById("status").appendChild(status_indicator);
166 if (rcnick() == 'anonymous') {
167 rcaddmessagetohistory("-!- Set your nick with /nick");
171 function rcsend(d, message) {
172 var id = new Date().getTime() + "-" + session + "-" + Math.random();
173 seen[make_seen_key(id, message)] = true;
174 var path = "/speak" +
175 "?id=" + encodeURIComponent(id) +
176 "&text=" + encodeURIComponent(message);
177 for (var i in servers) {
178 var uri = rcserverbase(servers[i]) + path;
179 var img = document.createElement("img");
180 img.setAttribute("src", uri);
185 function rckeydown(event) {
186 if (event.keyCode == 13) {
187 var input = document.input.say.value;
188 document.input.say.value = "";
192 var re = /^\/nick (.*)/;
193 var match = re.exec(input);
195 message = "*** " + rcnick() + " is now known as " + match[1];
198 message = "<" + rcnick() + "> " + input;
203 var re = /^\/me (.*)/;
204 var match = re.exec(input);
205 var inputme = input.substring(4);
207 message = "* " + rcnick() + " " + inputme;
209 message = "<" + rcnick() + "> " + input;
213 var d = rcaddmessagetohistory(message);
216 // Remind people to set their nick
217 if (rcnick() == 'anonymous') {
218 rcaddmessagetohistory("-!- Set your nick with /nick");
226 <body onload=
"rcconnect()">
228 <div id=
"history"></div>
231 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
232 <input id=
"say" onkeydown=
"return rckeydown(event)" autocomplete=
"off" autofocus=
"autofocus"></input>
234 <div id=
"status"> </div>