分享

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">WebTorrent教程</font></font>

 黄三岁大爱人生 2018-08-18

开始使用WebTorrent

WebTorrent是第一个在浏览器中工作的torrent客户端这很容易上手!

安装

要开始使用WebTorrent,只需webtorrent.min.js 在页面上包含脚本即可

<script src="webtorrent.min.js"></script>

这为对象提供了一个WebTorrent功能window

Browserify

WebTorrent也与伟大工程browserify,它可以让你使用Node.js的风格require()来组织你的浏览器的代码,并通过安装负载包NPM

npm install webtorrent

然后WebTorrent像这样使用

var WebTorrent = require('webtorrent')

快速举例

下载torrent(在浏览器中)

var WebTorrent = require('webtorrent') var client = new WebTorrent() // Sintel, a free, Creative Commons movie var torrentId = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2F%2Ftorrents%2F&xs=https%3A%2F%2F%2Ftorrents%2Fsintel.torrent' client.add(torrentId, function (torrent) { // Torrents can contain many files. Let's use the .mp4 file var file = torrent.files.find(function (file) { return file.name.endsWith('.mp4') }) // Display the file by adding it to the DOM. // Supports video, audio, image files, and more! file.appendTo('body') })

This supports video, audio, images, PDFs, Markdown, and more, right out of the box. There are additional ways to access file content directly, including as a node-style stream, Buffer, or Blob URL.

Video and audio content can be streamed, i.e. playback will start before the full file is downloaded. Seeking works too – WebTorrent dynamically fetches the needed torrent pieces from the network on-demand.

Creating a new torrent and seed it (in the browser)

var dragDrop = require('drag-drop') var WebTorrent = require('webtorrent') 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 (torrent) { console.log('Client is seeding ' + torrent.magnetURI) }) })

This example uses the drag-drop package, to make the HTML5 Drag and Drop API easier to work with.

Note: If you do not use browserify, use the standalone file dragdrop.min.js. This exports a DragDrop function on window.

Download and save a torrent (in Node.js)

var WebTorrent = require('webtorrent') var client = new WebTorrent() var magnetURI = 'magnet: ...' client.add(magnetURI, { path: '/path/to/folder' }, function (torrent) { torrent.on('done', function () { console.log('torrent download finished') }) })

Complete HTML page example

Looking for a more complete example? Look no further! This HTML example has a form input where the user can paste a magnet link and start a download over WebTorrent.

Best of all, it's a single HTML page, under 70 lines!

If the torrent contains images, videos, audio, or other playable files (with supported codecs), they will be added to the DOM and streamed, even before the full content is downloaded.

<!doctype html> <html> <body> <h1>Download files using the WebTorrent protocol (BitTorrent over WebRTC).</h1> <form> <label for="torrentId">Download from a magnet link: </label> <input name="torrentId", placeholder="magnet:" value="magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2F%2Ftorrents%2F&xs=https%3A%2F%2F%2Ftorrents%2Fsintel.torrent"> <button type="submit">Download</button> </form> <h2>Log</h2> <div class="log"></div> <!-- Include the latest version of WebTorrent --> <script src="https://cdn./webtorrent/latest/webtorrent.min.js"></script> <script> var client = new WebTorrent() client.on('error', function (err) { console.error('ERROR: ' + err.message) }) document.querySelector('form').addEventListener('submit', function (e) { e.preventDefault() // Prevent page refresh var torrentId = document.querySelector('form input[name=torrentId]').value log('Adding ' + torrentId) client.add(torrentId, onTorrent) }) function onTorrent (torrent) { log('Got torrent metadata!') log( 'Torrent info hash: ' + torrent.infoHash + ' ' + '<a href="' + torrent.magnetURI + '" target="_blank">[Magnet URI]</a> ' + '<a href="' + torrent.torrentFileBlobURL + '" target="_blank" download="' + torrent.name + '.torrent">[Download .torrent]</a>' ) // Print out progress every 5 seconds var interval = setInterval(function () { log('Progress: ' + (torrent.progress * 100).toFixed(1) + '%') }, 5000) torrent.on('done', function () { log('Progress: 100%') clearInterval(interval) }) // Render all files into to the page torrent.files.forEach(function (file) { file.appendTo('.log') log('(Blob URLs only work if the file is loaded from a server. "http//localhost" works. "file://" does not.)') file.getBlobURL(function (err, url) { if (err) return log(err.message) log('File done.') log('<a href="' + url + '">Download full file: ' + file.name + '</a>') }) }) } function log (str) { var p = document.createElement('p') p.innerHTML = str document.querySelector('.log').appendChild(p) } </script> </body> </html>

HTML example with status showing UI

This complete HTML example mimics the UI of the homepage. It downloads the sintel.torrent file, streams it in the browser and outputs some statistics to the user (peers, progress, remaining time, speed...).

You can try it right now on CodePen to see what it looks like and play around with it!

Feel free to replace torrentId with other torrent files, or magnet links, but keep in mind that the browser can only download torrents that are seeded by WebRTC peers (web peers). Use WebTorrent Desktop or Instant.io to seed torrents to the WebTorrent network.

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>WebTorrent video player</title> <style> #output video { width: 100%; } #progressBar { height: 5px; width: 0%; background-color: #35b44f; transition: width .4s ease-in-out; } body.is-seed .show-seed { display: inline; } body.is-seed .show-leech { display: none; } .show-seed { display: none; } #status code { font-size: 90%; font-weight: 700; margin-left: 3px; margin-right: 3px; border-bottom: 1px dashed rgba(255,255,255,0.3); } .is-seed #hero { background-color: #154820; transition: .5s .5s background-color ease-in-out; } #hero { background-color: #2a3749; } #status { color: #fff; font-size: 17px; padding: 5px; } a:link, a:visited { color: #30a247; text-decoration: none; } </style> </head> <body> <div id="hero"> <div id="output"> <div id="progressBar"></div> <!-- The video player will be added here --> </div> <!-- Statistics --> <div id="status"> <div> <span class="show-leech">Downloading </span> <span class="show-seed">Seeding </span> <code> <!-- Informative link to the torrent file --> <a id="torrentLink" href="https:///torrents/sintel.torrent">sintel.torrent</a> </code> <span class="show-leech"> from </span> <span class="show-seed"> to </span> <code id="numPeers">0 peers</code>. </div> <div> <code id="downloaded"></code> of <code id="total"></code> — <span id="remaining"></span><br/> &#x2198;<code id="downloadSpeed">0 b/s</code> / &#x2197;<code id="uploadSpeed">0 b/s</code> </div> </div> </div> <!-- Include the latest version of WebTorrent --> <script src="https://cdn./webtorrent/latest/webtorrent.min.js"></script> <!-- Moment is used to show a human-readable remaining time --> <script src="http:///downloads/moment.min.js"></script> <script> var torrentId = 'https:///torrents/sintel.torrent' var client = new WebTorrent() // HTML elements var $body = document.body var $progressBar = document.querySelector('#progressBar') var $numPeers = document.querySelector('#numPeers') var $downloaded = document.querySelector('#downloaded') var $total = document.querySelector('#total') var $remaining = document.querySelector('#remaining') var $uploadSpeed = document.querySelector('#uploadSpeed') var $downloadSpeed = document.querySelector('#downloadSpeed') // Download the torrent client.add(torrentId, function (torrent) { // Torrents can contain many files. Let's use the .mp4 file var file = torrent.files.find(function (file) { return file.name.endsWith('.mp4') }) // Stream the file in the browser file.appendTo('#output') // Trigger statistics refresh torrent.on('done', onDone) setInterval(onProgress, 500) onProgress() // Statistics function onProgress () { // Peers $numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers') // Progress var percent = Math.round(torrent.progress * 100 * 100) / 100 $progressBar.style.width = percent + '%' $downloaded.innerHTML = prettyBytes(torrent.downloaded) $total.innerHTML = prettyBytes(torrent.length) // Remaining time var remaining if (torrent.done) { remaining = 'Done.' } else { remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize() remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.' } $remaining.innerHTML = remaining // Speed rates $downloadSpeed.innerHTML = prettyBytes(torrent.downloadSpeed) + '/s' $uploadSpeed.innerHTML = prettyBytes(torrent.uploadSpeed) + '/s' } function onDone () { $body.className += ' is-seed' onProgress() } }) // Human readable bytes util function prettyBytes(num) { var exponent, unit, neg = num < 0, units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] if (neg) num = -num if (num < 1) return (neg ? '-' : '') + num + ' B' exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1) num = Number((num / Math.pow(1000, exponent)).toFixed(2)) unit = units[exponent] return (neg ? '-' : '') + num + ' ' + unit } </script> </body> </html>

More Documentation

Check out the API Documentation and FAQ for more details.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多