Skip to content

Instantly share code, notes, and snippets.

@Tercus
Created April 4, 2016 23:40
Show Gist options
  • Save Tercus/c0b2eb0f23999f0d91997fdcc9f5e2c8 to your computer and use it in GitHub Desktop.
Save Tercus/c0b2eb0f23999f0d91997fdcc9f5e2c8 to your computer and use it in GitHub Desktop.
<script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>
<script>
var client = new WebTorrent()
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
client.on('error', (err) => {
console.error('ERROR: ' + err.message)
})
document.querySelector('form').addEventListener('submit', function (e) {
e.preventDefault() // Prevent page refresh
var selectedFiles = document.querySelector('form input[name=file]').files
var fto = []
for (var i = 0; i < selectedFiles.length; i++) {
fto.push(selectedFiles.item(i))
}
console.log('Adding: ' + fto)
client.seed(fto, { announceList: [['https://localhost:8080'], ['udp://localhost:8080'], ['ws://localhost:8080']] })
})
client.on('torrent', function (torrent) {
//tell Node that it is supposed to download now.
xhttp.open('POST', 'http://localhost/upload', true)
xhttp.send(torrent.infoHash)
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment