Permalink
Browse files

fixed some bugs: long poll error after 60 secs, and delivery of join/…

…leave msgs
  • Loading branch information...
Dennis Homann
Dennis Homann committed Nov 15, 2009
1 parent a8dad7a commit 6d3a10013770236ee96a16d0fc7951f6eac033b4
Showing with 18 additions and 12 deletions.
  1. +2 −2 client.js
  2. +16 −10 server.js
View
@@ -18,7 +18,6 @@ function userJoin(nick, timestamp, text) {
for (var i = 0; i < nicks.length; i++)
if (nicks[i] == nick) return;
nicks.push(nick);
- updateUsersLink();
updateUsersDisplay();
}
@@ -29,6 +28,7 @@ function updateUsersDisplay() {
newList.append("<li>" + nick + "</li>");
}
$("#users ul").replaceWith(newList);
+ updateUsersLink();
}
function userPart(nick, timestamp, text) {
@@ -39,7 +39,7 @@ function userPart(nick, timestamp, text) {
break;
}
}
- updateUsersLink();
+ updateUsersDisplay();
}
// utility functions
View
@@ -8,7 +8,8 @@ var http = require("http");
var redis = require("./redis");
var MESSAGE_BACKLOG = 200;
-var SESSION_TIMEOUT = 60 * 1000;
+var CALLBACK_TIMEOUT = 30 * 1000;
+var SESSION_TIMEOUT = 2 * CALLBACK_TIMEOUT;
var CHAT_DB_NUMBER = 7;
var DEFAULT_CHANNEL = "default";
@@ -130,7 +131,7 @@ function createSession (nick) {
};
this.destroy = function () {
- this.channel.leave(this, "part", this.nick + " parted");
+ this.channel.leave(this, this.nick + " parted");
delete sessions[this.id];
};
@@ -152,15 +153,15 @@ function createSession (nick) {
if (messages.length > 0) {
callback(messages);
} else {
- s.callback = callback;
+ s.callback = { timestamp: new Date(), callback: callback };
}
});
}
};
this.deliver = function (messages) {
if (this.callback) {
- this.callback(messages);
+ this.callback.callback(messages || []);
this.callback = null;
}
};
@@ -179,10 +180,19 @@ function createSession (nick) {
this.systemMessages.push(message);
}
};
+
+ this.validate = function() {
+ var now = new Date();
+ if (now - this.timestamp > SESSION_TIMEOUT) {
+ this.destroy();
+ } else if (this.callback && (now - this.callback.timestamp > CALLBACK_TIMEOUT)) {
+ this.deliver();
+ }
+ };
};
sessions[session.id] = session;
- session.channel.join(session, "join", session.nick + " joined");
+ session.channel.join(session, session.nick + " joined");
return session;
}
@@ -191,11 +201,7 @@ setInterval(function () {
var now = new Date();
for (var id in sessions) {
if (!sessions.hasOwnProperty(id)) continue;
- var session = sessions[id];
-
- if (now - session.timestamp > SESSION_TIMEOUT) {
- session.destroy();
- }
+ sessions[id].validate();
}
}, 1000);

0 comments on commit 6d3a100

Please sign in to comment.