Skip to content
Browse files

Adding long polling to the console, added debug.log(str) to output to…

… the browser
  • Loading branch information...
1 parent eb46c77 commit 29dbc67b00f0c48bcc07e4532ebce88fc4e7e02b @Connorhd committed Sep 7, 2009
Showing with 79 additions and 0 deletions.
  1. +67 −0 debug.js
  2. +12 −0 index.html
View
67 debug.js
@@ -30,9 +30,76 @@ fu.get("/tree", function (req, res) {
} else {
res.simpleJSON(200, getObj('process'));
}
+});
+fu.get("/console", function (req, res) {
+ if (req.uri.params.id !== undefined) {
+ handleConsole(req.uri.params.id, res);
+ } else {
+ res.simpleText(200, 'Error');
+ }
});
+
+var SESSION_TIMEOUT = 60 * 1000;
+
+var sessions = [];
+
+function handleConsole(id, res) {
+ if (sessions[id] === undefined) {
+ // Create session
+ sessions[id] = {
+ timestamp: 0,
+ queue: []
+ };
+ }
+
+ var session = sessions[id];
+ session.timestamp = new Date();
+ if (session.queue.length > 0) {
+ res.simpleJSON(200, session.queue);
+ session.queue = [];
+ } else {
+ session.res = res;
+ session.timeout = setTimeout(function () { closeReq(res) }, 30000);
+ }
+}
+
+function closeReq(res) {
+ res.simpleJSON(200, []);
+}
+
+debug.log = function (msg) {
+ for (var id in sessions) {
+ if (!sessions.hasOwnProperty(id)) continue;
+ var session = sessions[id];
+
+ session.queue.push(msg);
+
+ if (session.timeout !== undefined) {
+ clearTimeout(session.timeout);
+ }
+
+ if (session.res !== undefined) {
+ session.res.simpleJSON(200, session.queue);
+ session.queue = [];
+ }
+ }
+}
+
+// interval to kill off old sessions
+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) {
+ delete sessions[id];
+ }
+ }
+}, 1000);
+
function getObj(key) {
var obj = process;
var keys = key.split('.');
View
12 index.html
@@ -43,6 +43,18 @@
unique: true
});
}
+
+ var id = Math.floor(Math.random()*10000);
+ function longPollConsole() {
+ $.getJSON("/console?id="+id,
+ function(data){
+ $.each(data, function(i, x) {
+ $("#console").prepend('<pre class="result">'+x+'</pre>');
+ });
+ longPollConsole();
+ });
+ }
+ longPollConsole();
</script>
<style type="text/css">

0 comments on commit 29dbc67

Please sign in to comment.
Something went wrong with that request. Please try again.