Skip to content

Instantly share code, notes, and snippets.

@auroranockert
Created December 1, 2011 22:15
Show Gist options
  • Save auroranockert/1420293 to your computer and use it in GitHub Desktop.
Save auroranockert/1420293 to your computer and use it in GitHub Desktop.
class HTTPSource
constructor: (@name) ->
@chunkSize = 1 * 1024 * 1024
@outputs = {}
this.reset()
start: () ->
@status = "Started"
if @inflight
console.log("Should never get here, unless you're starting a stream with in-flight requests"); debugger
return this.loop()
pause: () ->
@status = "Paused"
if @inflight
@xhr.abort()
@inflight = false
return this
reset: () ->
@status = "Paused"
@xhr.abort() if @inflight
@offset = 0
@inflight = false
return this
loop: () ->
if @inflight
console.log("Should never be here, unless a loop is failing"); debugger
@inflight = true
onProgress = (event) =>
console.log("HTTP Progress: #{event.loaded}/#{@chunkSize / 1024}kB")
return
onLoad = (event) =>
console.log("HTTP Finished: #{@name}")
buffer = new Buffer(new Uint8Array(@xhr.response))
@outputs.contents.send(buffer)
@offset += buffer.length
@inflight = false
this.loop()
return
onError = (event) =>
console.log("HTTP Error: ", event)
@inflight = false
this.pause()
@messagebus.send(this, @name, "ERROR", "Source paused, errror sending HTTP request")
return
onAbort = (event) =>
console.log("HTTP Aborted: Paused?")
return
@xhr = new XMLHttpRequest()
@xhr.addEventListener("progress", onProgress, false);
@xhr.addEventListener("load", onLoad, false);
@xhr.addEventListener("error", onError, false);
@xhr.addEventListener("abort", onAbort, false);
@xhr.open("GET", @url, true)
@xhr.responseType = "arraybuffer"
# @xhr.setRequestHeader("Range", "bytes=#{@offset}-#{@offset + @chunkSize}");
return this
window.Aurora = {} unless window.Aurora
window.Aurora.HTTPSource = HTTPSource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment