Skip to content

Instantly share code, notes, and snippets.

@weepy
Created September 12, 2011 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weepy/1211175 to your computer and use it in GitHub Desktop.
Save weepy/1211175 to your computer and use it in GitHub Desktop.
stream ogg via node
var http = require('http'),
fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, 'music/Shakira - Waka Waka (Time For Africa).ogg'),
stat = fs.statSync(filePath)
file = fs.readFileSync(filePath),
chunk = 1000
http.createServer(function(req, res) {
var range = req.headers.range,
total = file.length,
parts = range.replace(/bytes=/, "").split("-"),
start = parseInt(parts[0], 10),
end = parts[1] ? parseInt(parts[1], 10) : (start+chunk)
if(end > file.length) end = file.length
var chunksize = (end-start);
console.log(range, start, end, chunksize)
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-type": "audio/ogg"
});
res.end(file.slice(start, end), "binary");
})
.listen(2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment