Created
April 1, 2014 18:39
-
-
Save aj0strow/9920286 to your computer and use it in GitHub Desktop.
Streaming Music
This file contains 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 queue = []; | |
var audio = new Audio(); | |
audio.controls = true; | |
audio.autoplay = true; | |
audio.addEventListener('ended', function (ev) { | |
console.log('next song'); | |
playNext(); | |
}); | |
function playNext () { | |
var url = queue.shift(); | |
if (url) { | |
audio.src = url; | |
} | |
} | |
window.ws.on('stream', function (stream, meta) { | |
console.log('stream event', meta); | |
var chunks = []; | |
stream.on('data', chunks.push.bind(chunks)); | |
stream.on('end', function () { | |
var blob = new Blob(chunks); | |
var url = (window.URL || window.webkitURL).createObjectURL(blob); | |
queue.push(url); | |
if (audio.paused) playNext(); | |
}); | |
}); | |
$(function () { | |
var $upload = document.getElementById('upload'); | |
$upload.addEventListener('dragover', function (ev) { | |
ev.stopPropagation(); | |
ev.preventDefault(); | |
ev.dataTransfer.dropEffect = 'copy'; | |
}, false); | |
$upload.addEventListener('drop', function (ev) { | |
ev.stopPropagation(); | |
ev.preventDefault(); | |
var files = ev.dataTransfer.files; | |
$.each(files, function (index, file) { | |
var meta = { name: file.name, size: file.size }; | |
console.log(meta); | |
window.ws.send(file, meta); | |
}); | |
}, false); | |
}); |
This file contains 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 http = require('http'); | |
var fs = require('fs'); | |
var binaryjs = require('binaryjs'); | |
var server = http.createServer(function (req, res) { | |
fs.createReadStream('index.html').pipe(res); | |
}); | |
var ws = new binaryjs.BinaryServer({ server: server, path: '/media' }); | |
ws.on('connection', function (client) { | |
console.log('new client'); | |
client.on('stream', function (stream, meta) { | |
console.log('receiving %s', meta.name); | |
stream.on('end', function () { | |
console.log('queue %s', meta.name); | |
broadcast(meta.name); | |
}); | |
stream.pipe(fs.createWriteStream(path(meta.name))); | |
}); | |
}); | |
function path (filename) { | |
return 'tmp/' + filename; | |
} | |
function broadcast (filename) { | |
Object.keys(ws.clients).forEach(function (clientid) { | |
console.log('broadcast %s', clientid); | |
var stream = fs.createReadStream(path(filename)); | |
stream.pipe(ws.clients[clientid].createStream({ name: filename })); | |
}); | |
} | |
var port = process.env.PORT || 9000; | |
app.listen(port); | |
console.log('localhost:%s', port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment