Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code examples #538

Merged
merged 4 commits into from Dec 27, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -147,7 +147,7 @@ var client = new WebTorrent()
// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', function (files) {
client.seed(files, function onTorrent (torrent) {
client.seed(files, function (torrent) {
console.log('Client is seeding:', torrent.infoHash)
})
})
@@ -2,24 +2,12 @@ var WebTorrent = require('webtorrent')

var client = new WebTorrent()

// Go to https://instant.io, seed a file and use the magnet uri generated
var magnetUri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'

client.add(magnetUri, function (torrent) {
// Got torrent metadata!
console.log('Torrent info hash:', torrent.infoHash)
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the first.
var file = torrent.files[0]

torrent.files.forEach(function (file) {
// Get a url for each file
file.getBlobURL(function (err, url) {
if (err) throw err

// Add a link to the page
var a = document.createElement('a')
a.download = file.name
a.href = url
a.textContent = 'Download ' + file.name
document.body.appendChild(a)
})
})
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('body')
})
@@ -5,8 +5,8 @@ var client = new WebTorrent()

// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', function (files) {
client.seed(files, function onTorrent (torrent) {
client.seed(files, function (torrent) {
// Client is seeding the file!
console.log('Torrent info hash:', torrent.infoHash)
console.log('Torrent magnet link:', torrent.magnetURI)
})
})

This file was deleted.

@@ -0,0 +1,74 @@
<!doctype html>
<html>
<head>
<title>WebTorrent – Simple File Sending Example</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Streaming File Transfer over <a href="http://webtorrent.io">WebTorrent</a></h1>

<p>Download files using the WebTorrent protocol (BitTorrent over WebRTC).</p>

<h2>Log</h2>
<div class="log"></div>

<h2>Start downloading</h2>

<form>
<label for="torrentId">Download from a magnet link or info hash</label>
<input name="torrentId", placeholder="magnet:">
<button type="submit">Download</button>
</form>

<br>
<p><small>Code is available on <a href="https://github.com/feross/instant.io">GitHub</a> under MIT License. Run <code>localStorage.debug = '*'</code> in the console and refresh to enable verbose logs.</small></p>

<!-- Include the latest version of WebTorrent -->
<script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>

<script>
var client = new WebTorrent()

// Print warnings and errors to the console
client.on('warning', function (err) {
console.log('WARNING: ' + err.message)
})
client.on('error', function (err) {
console.error('ERROR: ' + err.message)
})

document.querySelector('form').addEventListener('submit', onSubmit)

function onSubmit (e) {
e.preventDefault() // Prevent page refresh

var torrentId = document.querySelector('form input[name=torrentId]').value
client.add(torrentId, onTorrent)
}

function onTorrent (torrent) {
alert('torrent')
log(
'Torrent info hash: ' + torrent.infoHash + ' ' +
'<a href="' + torrent.magnetURI + '" target="_blank">[Magnet URI]</a> ' +
'<a href="' + torrent.torrentFileURL + '" target="_blank" download="' + torrentFileName + '">[Download .torrent]</a>'
)

torrent.files.forEach(function (file) {
file.appendTo('.log')
})
}

function log (str) {
var p = document.createElement('p')
p.innerHTML = str
document.querySelector('.log').appendChild(p)
}

</script>
</body>
</html>
@@ -2,11 +2,11 @@ var WebTorrent = require('webtorrent')
var fs = require('fs')

var client = new WebTorrent()
var magnetUri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
var magnetUri = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'

client.download(magnetUri, function (torrent) {
// Got torrent metadata!
console.log('Torrent info hash:', torrent.infoHash)
console.log('Torrent magnet link:', torrent.magnetURI)

torrent.files.forEach(function (file) {
// Stream each file to the disk
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.