Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…

var transferInProgress = false; | |
Pebble.addEventListener("ready", function(e) { | |
console.log("NetImage JS Ready"); | |
}); | |
Pebble.addEventListener("appmessage", function(e) { | |
console.log("Got message: " + JSON.stringify(e)); | |
if ('NETIMAGE_URL' in e.payload) { | |
if (transferInProgress == false) { | |
transferInProgress = true; | |
downloadBinaryResource(e.payload['NETIMAGE_URL'], function(bytes) { | |
transferImageBytes(bytes, e.payload['NETIMAGE_CHUNK_SIZE'], | |
function() { console.log("Done!"); transferInProgress = false; }, | |
function(e) { console.log("Failed! " + e); transferInProgress = false; } | |
); | |
}); | |
} | |
else { | |
console.log("Ignoring request to download " + e.payload['NETIMAGE_URL'] + " because another download is in progress."); | |
} | |
} | |
}); | |
function downloadBinaryResource(imageURL, callback, errorCallback) { | |
var req = new XMLHttpRequest(); | |
req.open("GET", imageURL,true); | |
req.responseType = "arraybuffer"; | |
req.onload = function(e) { | |
console.log("loaded"); | |
var buf = req.response; | |
if(req.status == 200 && buf) { | |
var byteArray = new Uint8Array(buf); | |
var arr = []; | |
for(var i=0; i<byteArray.byteLength; i++) { | |
arr.push(byteArray[i]); | |
} | |
console.log("Received image with " + byteArray.length + " bytes."); | |
callback(arr); | |
} | |
else { | |
errorCallback("Request status is " + req.status); | |
} | |
} | |
req.onerror = function(e) { | |
errorCallback(e); | |
} | |
req.send(null); | |
} | |
function transferImageBytes(bytes, chunkSize, successCb, failureCb) { | |
var retries = 0; | |
success = function() { | |
console.log("Success cb=" + successCb); | |
if (successCb != undefined) { | |
successCb(); | |
} | |
}; | |
failure = function(e) { | |
console.log("Failure cb=" + failureCb); | |
if (failureCb != undefined) { | |
failureCb(e); | |
} | |
}; | |
// This function sends chunks of data. | |
sendChunk = function(start) { | |
var txbuf = bytes.slice(start, start + chunkSize); | |
console.log("Sending " + txbuf.length + " bytes - starting at offset " + start); | |
Pebble.sendAppMessage({ "NETIMAGE_DATA": txbuf }, | |
function(e) { | |
// If there is more data to send - send it. | |
if (bytes.length > start + chunkSize) { | |
sendChunk(start + chunkSize); | |
} | |
// Otherwise we are done sending. Send closing message. | |
else { | |
Pebble.sendAppMessage({"NETIMAGE_END": "done" }, success, failure); | |
} | |
}, | |
// Failed to send message - Retry a few times. | |
function (e) { | |
if (retries++ < 3) { | |
console.log("Got a nack for chunk #" + start + " - Retry..."); | |
sendChunk(start); | |
} | |
else { | |
failure(e); | |
} | |
} | |
); | |
}; | |
// Let the pebble app know how much data we want to send. | |
Pebble.sendAppMessage({"NETIMAGE_BEGIN": bytes.length }, | |
function (e) { | |
// success - start sending | |
sendChunk(0); | |
}, failure); | |
} |