]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.js
b2860225f6b90cdcf881282ac70672d45eee58a7
1 var servers
= ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
3 var session
= Math
.random();
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";
11 return "http://" + server
;
15 function rcchangeserverstatus(server
, new_status
) {
16 var statusbar
= document
.getElementById("status");
17 var spans
= statusbar
.getElementsByTagName("span");
19 if (spans
[i
].firstChild
&& 'data' in spans
[i
].firstChild
&& spans
[i
].firstChild
.data
== server
) {
20 spans
[i
].setAttribute("class", new_status
);
25 function rcaddmessagetohistory(message
) {
26 var d
= document
.createElement("div");
27 d
.appendChild(document
.createTextNode(message
));
28 var h
= document
.getElementById("history");
30 window
.scrollTo(0, document
.body
.scrollHeight
);
34 function make_seen_key(id
, text
) {
35 return id
.replace(/@/g
, "@@") + "_@_" + text
.replace(/@/g
, "@@");
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
);
44 rcchangeserverstatus(servers
[i
], "sad");
47 rcchangeserverstatus(server
, "happy");
50 function receiveMessageEvent(event
)
53 if (event
.origin
=== rcserverbase(servers
[i
])) {
54 messages
= JSON
.parse(event
.data
);
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']);
66 function rcconnect() {
67 window
.addEventListener("message", receiveMessageEvent
, false);
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
);
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
);
81 function rcsend(d
, message
) {
82 var id
= new Date().getTime() + "-" + session
+ "-" + Math
.random();
83 seen
[make_seen_key(id
, message
)] = true;
85 "?id=" + encodeURIComponent(id
) +
86 "&text=" + encodeURIComponent(message
);
88 var uri
= rcserverbase(servers
[i
]) + path
;
89 var img
= document
.createElement("img");
90 img
.setAttribute("src", uri
);
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
= "";