Skip to content

Instantly share code, notes, and snippets.

@tmzt tmzt/gist:974100
Created May 16, 2011

Embed
What would you like to do?
sendevents.js
/* polling */
app.get('/apis/v0/events.json', getevents);
var clients = {};
var queues = {};
var shorturlsessionid = {};
function getevents(req, res) {
shorturlsessionid['timothy'] = req.sessionID;
if (typeof(clients[req.sessionID]) == 'undefined') clients[req.sessionID] = {requests: []};
var queue = queues[req.sessionID];
if (typeof(queue) == 'undefined') queue = [];
if (queue && queue.length) {
queues[req.sessionID] = [];
res.send(queue);
} else {
if (!clients[req.sessionID].requests.length) {
console.log('pushing request');
console.log('sessionID: ', req.sessionID);
clients[req.sessionID].requests.push(res);
};
};
};
function sendevents(req, res, callback) { sendeventsto(req.sessionID, callback); };
function sendeventsto(sessionid, callback) {
console.log('sending events to: ', sessionid);
var client = clients[sessionid];
if (typeof(client) == 'undefined') client = null;
var queue = queues[sessionid];
if (typeof(queue) == 'undefined') queue = queues[sessionid] = [];
if (client != null) {
var requests = client.requests;
requests.forEach(function(request) {
console.log('queue: ', queue);
if (queue.length) {
console.log('sending queue: ', queue);
request.send(queue);
} else { request.send(null); };
});
clients[sessionid].requests = [];
};
callback(true);
};
function queueevent(req, res, event) { queueeventfor(req.sessionID, event); };
function queueeventfor(sessionid, event) {
if (typeof(queues[sessionid]) == 'undefined') queues[sessionid] = [];
console.log('queues[sessionid]: ', queues[sessionid]);
console.log('event: ', event);
queues[sessionid].push(event);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.