- function receiveMessageEvent(event)
- {
- for (var i in servers) {
- if (event.origin === rcserverbase(servers[i])) {
- messages = JSON.parse(event.data);
- for (var j in messages) {
- if ('Time' in messages[j] &&
- 'ID' in messages[j] &&
- 'Text' in messages[j]) {
- receiveMessage(servers[i], messages[j]['Time'], messages[j]['ID'], messages[j]['Text']);
+ function rcreceivemessages(server, messages) {
+ for (var i in messages) {
+ var m = messages[i];
+ m.Time = new Date(m.Time);
+ var seen_key = make_seen_key(m.ID, m.Text);
+ if (seen_key in seen) {
+ seen[seen_key].ServerTimes[server] = m.Time;
+ rcupdatemessagetime(seen[seen_key]);
+ } else {
+ m.ServerTimes = {};
+ m.ServerTimes[server] = m.Time;
+ seen[seen_key] = m;
+ rcaddmessagetohistory(m);
+ for (var i in servers) {
+ rcchangeserverstatus(servers[i], "sad");
+ }
+ }
+ rcchangeserverstatus(server, "happy");
+ }
+ }
+
+ function rcfetch(server) {
+ var delay = 10000; // TODO: Exponential backoff
+ var xhr = new XMLHttpRequest();
+ xhr.onreadystatechange = function() {
+ if (this.readyState == this.DONE) {
+ if (this.status == 200) {
+ var rtxt = this.responseText;
+ if (rtxt != null) {
+ var messages = JSON.parse(rtxt);
+ if (messages != null) {
+ delay = 40;
+ if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
+ since[server] = messages[messages.length-1].Time;
+ }
+ rcreceivemessages(server, messages);
+ }