public
Last active

  • Download Gist
long_pull_client.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
var LongPuller = function() {
var internals = {};
internals.subscribers = {};
internals.xhrs = {};
internals.doXHR = function(key, url, errorTimeout, reconnectTimeout) {
var xhr = new XMLHttpRequest();
xhr.myCustomData = { key: key, url: url, errorTimeout: errorTimeout || 5000, reconnectTimeout: reconnectTimeout || 100 };
xhr.onreadystatechange = internals.handleResponse;
xhr.open("GET", url);
xhr.send();
internals.xhrs[key] = xhr;
};
internals.handleResponse = function() {
if(this.readyState != 4) { return; }
if(this.status == 200) {
internals.fire(this.myCustomData.key, { url: this.myCustomData.url, response: this.responseText });
sleep(this.myCustomData.reconnectTimeout);
} else {
sleep(this.myCustomData.errorTimeout);
}
internals.doXHR(this.myCustomData.key, this.myCustomData.url);
};
internals.stop = function(key) {
if(internals.xhrs[key]) {
internals.xhrs[key].abort();
}
};
internals.fire = function(key, value) {
if(!internals.subscribers[key]) {
log("No subscribers for '", key, "'!");
log(value.toSource());
return;
}
for (var index in internals.subscribers[key]) {
if(internals.subscribers[key][index] instanceof Function) {
internals.subscribers[key][index](value);
}
}
};
return {
start: internals.doXHR,
stop: internals.stop,
_subscribers: internals.subscribers
};
}();
long_pull_server.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// do our includes
var http = require('http'),
events = require('events');
// setup some local variables we need
var port = process.env.PORT || 8001,
dataStore = new events.EventEmitter(),
mostRecentStored;
 
// create our HTTP server which will listen for requests
http.createServer(function (req, res) {
// check to see if this is a store request, if it is, handle as such
if(req.url.indexOf("/store?") == 0) {
var query = require('url').parse(req.url).query;
mostRecentStored = query;
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('OK - ' + query);
dataStore.emit("store", query);
} else {
// see if we have a saved value which was never sent to a receiving client
if(mostRecentStored) {
// we have a saved value, so send it closing the connection
// then clear the variable so another request doesn't get it
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end(mostRecentStored);
mostRecentStored = null;
} else {
// we don't have a saved value waiting, so we must wait for someone to store one
dataStore.addListener('store', function(value) {
// we got one! now send it to the client
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end(value);
// we don't want to send the same value twice, so clear out the saved value
mostRecentStored = null;
});
}
}
 
// listen on the PORT defined in the environment or 8001 if not provided
}).listen(parseInt(port))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.