Skip to content

Commit

Permalink
Adding long polling to the console, added debug.log(str) to output to…
Browse files Browse the repository at this point in the history
… the browser
  • Loading branch information
Connorhd committed Sep 7, 2009
1 parent eb46c77 commit 29dbc67
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
67 changes: 67 additions & 0 deletions debug.js
Expand Up @@ -30,9 +30,76 @@ fu.get("/tree", function (req, res) {
} else { } else {
res.simpleJSON(200, getObj('process')); 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) { function getObj(key) {
var obj = process; var obj = process;
var keys = key.split('.'); var keys = key.split('.');
Expand Down
12 changes: 12 additions & 0 deletions index.html
Expand Up @@ -43,6 +43,18 @@
unique: true 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> </script>


<style type="text/css"> <style type="text/css">
Expand Down

0 comments on commit 29dbc67

Please sign in to comment.