Last active
August 29, 2015 13:56
-
-
Save apla/8884574 to your computer and use it in GitHub Desktop.
sending packets of data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PacketSender = function (url, options) { | |
this.data = []; | |
this.url = url; | |
options = options || {}; | |
if (!options.maxPacketsToSend) | |
options.maxPacketsToSend = 100; | |
if (!options.timeout) | |
options.timeout = 60000; | |
this.o = options; | |
this.queuedPackets = {}; | |
} | |
PacketSender.prototype.addPacket = function (packet) { | |
this.data.push (packet); | |
}; | |
PacketSender.prototype.onReadyStateChange = function(requestId, evt) { | |
var req = evt.target; | |
if (req.readyState == 4) { | |
if (req.status == 200) | |
delete this.queuedPackets[requestId]; | |
// alert(req.responseText); | |
else if (req.status) { | |
this.onError(requestId, evt); | |
} | |
//alert("Error loading page\n"); | |
} | |
} | |
PacketSender.prototype.onError = function (requestId, evt) { | |
console.log(requestId, evt); | |
var failedPackets = this.queuedPackets[requestId]; | |
delete this.queuedPackets[requestId] | |
//this.data.unshift.apply (failedPackets); | |
this.data.unshift.apply(this.data, failedPackets); | |
} | |
PacketSender.prototype.createXHR = function (method, url) { | |
if (!url && method) { | |
url = method; | |
method = "GET"; | |
} | |
var xhr = new XMLHttpRequest(); | |
//xhr.onload = reqListener; | |
xhr.open("POST", url, true); | |
//xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); | |
//xhr.send(JSON.stringify (kinectDataForReq)); | |
return xhr; | |
}; | |
PacketSender.prototype.sendPackets = function (xhr, timeout) { | |
var self = this; | |
if (this.o.noEmptyRequest && this.data.length == 0) { | |
console.log ('no data to send'); | |
return; | |
} | |
if (!xhr) { | |
if (this.url) { | |
xhr = this.createXHR (this.method, this.url); | |
} else { | |
console.error ('you must provide existing xhr object or url to sendPackets ' | |
+'or define url via constructor new PacketSender (url, {"maxPacketsToSend": 100})'); | |
} | |
} else if (xhr instanceof XMLHttpRequest) { | |
} else if (("string" == typeof xhr) || (xhr instanceof String)) { | |
var url = xhr; | |
xhr = this.createXHR (arguments); | |
} | |
var packetsToSend = this.data.splice (0, ( | |
this.o.maxPacketsToSend > this.data.length | |
? this.data.length | |
: this.o.maxPacketsToSend | |
)); | |
var requestId = new Date().getTime(); | |
// TODO: check for overwrite | |
this.queuedPackets[requestId] = packetsToSend; | |
if (this.o.dataWrapper && typeof this.o.dataWrapper === "function") { | |
packetsToSend = this.o.dataWrapper (packetsToSend); | |
} | |
if (this.o.xhrWrapper && typeof this.o.xhrWrapper === "function") { | |
this.o.xhrWrapper (xhr); | |
} | |
xhr.timeout = this.timeout || timeout; | |
xhr.addEventListener ("error", this.onError.bind (this, requestId), false); | |
xhr.ontimeout = this.onError.bind (this, requestId); | |
xhr.onreadystatechange = this.onReadyStateChange.bind (this, requestId); | |
xhr.send(JSON.stringify (packetsToSend)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var options = { | |
maxPacketsToSend: 10, | |
xhrWrapper: function(oReq) { | |
//var username = "kinect"; | |
//var password = "sdlkg3"; | |
var username = "hack"; | |
var password = "thegibson"; | |
oReq.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); | |
} | |
}; | |
var packetSender = new PacketSender('http://apla.me:5000', options); | |
for (i = 0; i < 100; i++) { | |
packetSender.addPacket({id: i}); | |
} | |
timer = setInterval(function() { | |
packetSender.sendPackets(); | |
if (!packetSender.data.length) | |
clearInterval(timer); | |
}, 2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment