]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.js
b2860225f6b90cdcf881282ac70672d45eee58a7
[reliable-chat] / webclient / rc.js
1 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
2
3 var session = Math.random();
4 var seen = {};
5
6 function rcserverbase(server) {
7 // Add the default port if server doesn't contain a port number already
8 if (server.indexOf(":") == -1) {
9 return "http://" + server + ":21059";
10 } else {
11 return "http://" + server;
12 }
13 }
14
15 function rcchangeserverstatus(server, new_status) {
16 var statusbar = document.getElementById("status");
17 var spans = statusbar.getElementsByTagName("span");
18 for (i in spans) {
19 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
20 spans[i].setAttribute("class", new_status);
21 }
22 }
23 }
24
25 function rcaddmessagetohistory(message) {
26 var d = document.createElement("div");
27 d.appendChild(document.createTextNode(message));
28 var h = document.getElementById("history");
29 h.appendChild(d);
30 window.scrollTo(0, document.body.scrollHeight);
31 return d;
32 }
33
34 function make_seen_key(id, text) {
35 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
36 }
37
38 function receiveMessage(server, time, id, text) {
39 var seen_key = make_seen_key(id, text);
40 if (!(seen_key in seen)) {
41 seen[seen_key] = true;
42 rcaddmessagetohistory(text);
43 for (i in servers) {
44 rcchangeserverstatus(servers[i], "sad");
45 }
46 }
47 rcchangeserverstatus(server, "happy");
48 }
49
50 function receiveMessageEvent(event)
51 {
52 for (i in servers) {
53 if (event.origin === rcserverbase(servers[i])) {
54 messages = JSON.parse(event.data);
55 for (j in messages) {
56 if ('Time' in messages[j] &&
57 'ID' in messages[j] &&
58 'Text' in messages[j]) {
59 receiveMessage(servers[i], messages[j]['Time'], messages[j]['ID'], messages[j]['Text']);
60 }
61 }
62 }
63 }
64 }
65
66 function rcconnect() {
67 window.addEventListener("message", receiveMessageEvent, false);
68 for (i in servers) {
69 // Create a hidden iframe for same-origin workaround
70 var iframe = document.createElement("iframe");
71 iframe.setAttribute("src", rcserverbase(servers[i]) + "/frame");
72 document.body.insertBefore(iframe, document.body.firstChild);
73 // Status bar entry
74 var status_indicator = document.createElement("span");
75 status_indicator.appendChild(document.createTextNode(servers[i]));
76 status_indicator.setAttribute("class", "sad");
77 document.getElementById("status").appendChild(status_indicator);
78 }
79 }
80
81 function rcsend(d, message) {
82 var id = new Date().getTime() + "-" + session + "-" + Math.random();
83 seen[make_seen_key(id, message)] = true;
84 var path = "/speak" +
85 "?id=" + encodeURIComponent(id) +
86 "&text=" + encodeURIComponent(message);
87 for (i in servers) {
88 var uri = rcserverbase(servers[i]) + path;
89 var img = document.createElement("img");
90 img.setAttribute("src", uri);
91 d.appendChild(img);
92 }
93 }
94
95 function rckeydown(event) {
96 if (event.keyCode == 13) {
97 var d = rcaddmessagetohistory(document.input.say.value);
98 rcsend(d, document.input.say.value);
99 document.input.say.value = "";
100 return false;
101 }
102 }