WebTorrent是Node.js和浏览器的流式洪流客户端。是的,是的。浏览器。它完全用JavaScript(网络的语言)编写,因此在两个运行时都可以使用相同的代码。
在Node.js中,此模块是一个简单的Torrent客户端,使用TCP和UDP与其他Torrent客户端进行交谈。
在浏览器中,WebTorrent使用WEBRTC (数据通道)进行点对点传输。可以在没有浏览器插件,扩展名或安装的情况下使用它。只是JavaScript™。注意:WebTorrent不支持浏览器中的UDP/TCP对等。
只需在您的页面上包含webtorrent.min.js脚本即可使用Bittorrent协议开始通过WEBRTC获取文件,或使用browsererify或webpack import WebTorrent from 'webtorrent' 。请参阅下面的演示应用程序和代码示例。
为了使Bittorrent对WebRTC(这是在网络上使用的P2P传输)进行的,我们进行了一些协议更改。因此,基于浏览器的WebTorrent客户端或“ Web Peer”只能连接到支持WebTorrent/WEBRTC的其他客户端。
要向网络同行播种文件,请使用支持WebTorrent的客户端,例如Web Torrent桌面,一个桌面客户端,该客户端具有熟悉的UI,可以连接到Web Peers,WebTorrent-Hybrid,命令行程序或Instant.io,网站,网站。像Vuze这样的洪流客户端已经添加了WebTorrent支持,因此他们可以连接到正常和网络同行。我们希望其他客户能遵循。
<video>标签( webm, mkv, mp4, ogv, mov, etc (AV1, H264, HEVC*, VP8, VP9, AAC, FLAC, MP3, OPUS, Vorbis, etc) )要在节点或import WebTorrent from 'webtorrent' ,请运行:
npm install webtorrent要安装webtorrent命令行程序,请运行:
npm install webtorrent-cli -g要安装用于Mac,Windows或Linux的Web Torrent桌面应用程序,请参见WebTorrent桌面。
#webtorrent的Gitter或FreeNode,以帮助开发或与一些疯狂的科学黑客闲逛:)很多人!
阅读完整的API文档。
WebTorrent是第一个使用打开的Web标准(无插件,仅HTML5和WebRTC)在浏览器中工作的BitTorrent客户端!很容易开始!
import WebTorrent from 'webtorrent'
const client = new WebTorrent ( )
const magnetURI = '...'
client . add ( magnetURI , torrent => {
// Got torrent metadata!
console . log ( 'Client is downloading:' , torrent . infoHash )
for ( const file of torrent . files ) {
document . body . append ( file . name )
}
} ) import dragDrop from 'drag-drop'
import WebTorrent from 'webtorrent'
const client = new WebTorrent ( )
// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop ( 'body' , files => {
client . seed ( files , torrent => {
console . log ( 'Client is seeding:' , torrent . infoHash )
} )
} )文档/get-Started.md中还有更多示例。
WebTorrent与NPM软件包的浏览效果很好,它使您可以使用Node-style require()来组织NPM安装的浏览器代码和加载模块(如先前的示例所示)。
WebTorrent还与另一个模块Bundler WebPack合作。但是,WebPack需要额外的配置,您可以在WebTorrent使用的WebPack Bundle配置中找到。
或者,您只能通过import WebTorrent from 'webtorrent/dist/webtorrent.min.js'使用预构建的版本,然后跳过WebPack配置。
WebTorrent也可作为独立脚本( webtorrent.min.js )提供,该脚本在window对象上揭示WebTorrent ,因此可以仅与脚本标签一起使用:
< script type =' module ' >
import WebTorrent from 'webtorrent.min.js'
</ script >WebTorrent脚本还托管在快速,可靠的CDN基础架构(CloudFlare和MaxCDN)上,以便于您的网站包含:
< script type =' module ' >
import WebTorrent from 'https://esm.sh/webtorrent'
</ script > 如果要在Chrome应用中使用WebTorrent,则可以包括以下脚本:
< script type =' module ' >
import WebTorrent from 'webtorrent.chromeapp.js'
</ script >确保启用chrome.sockets.udp和chrome.sockets.tcp权限!
WebTorrent还使用相同的NPM软件包在Node.js中工作!这是疯狂的科学!
注意:要连接到“ Web Peers”(浏览器)之外,除了普通的Bittorrent同行外,请使用Web Torrent-Hybrid,其中包括对节点的WebRTC支持。
WebTorrent也可作为命令行应用程序提供。这是使用它的方法:
$ npm install webtorrent-cli -g
$ webtorrent --help下载洪流:
$ webtorrent magnet_uri要将洪流流向诸如Airplay或Chromecast之类的设备,只需传递标志:
$ webtorrent magnet_uri --airplay有许多支持的流选项:
--airplay Apple TV
--chromecast Chromecast
--mplayer MPlayer
--mpv MPV
--omx [jack] omx [default: hdmi]
--vlc VLC
--xbmc XBMC
--stdout standard out [implies --quiet]除了磁铁URIS外,WebTorrent还支持许多指定洪流的方法。
大多数主动开发都发生在WebTorrent使用的小型NPM软件包内部。
“当应用程序做得好时,它们只是非常特定于应用程序的,咸淡的残留物,不能那么容易抽象出来。所有可重复使用的组件都可以升华到github和npm上,每个人都可以在这里协作以推进下议院。” - 用“我如何编写模块”代替
这些是组成WebTorrent的主要模块:
| 模块 | 测试 | 版本 | 描述 |
|---|---|---|---|
| Webtorrent | 洪流客户端(此模块) | ||
| Bittorrent-DHT | 分布式哈希表客户端 | ||
| Bittorrent-Peerid | 标识客户名称/版本 | ||
| Bittorrent-protocol | Bittorrent协议流 | ||
| Bittorrent-Tracker | Bittorrent Tracker服务器/客户端 | ||
| Bittorrent-LSD | Bittorrent本地服务发现 | ||
| 创建torrent | 创建.torrent文件 | ||
| 磁性uri | 解析磁铁URI | ||
| 解析二线 | 解析洪流标识符 | ||
| 洪流发现 | 通过DHT,Tracker和LSD查找同行 | ||
| UT_Metadata | 磁铁URI的元数据(协议扩展) | ||
| UT_PEX | 同行发现(协议扩展) |
在节点中,通过将DEBUG环境变量设置为要调试的模块的名称(例如bittorrent-protocol或*打印所有日志)来启用调试日志。
DEBUG= * webtorrent在浏览器中,通过在开发人员控制台中运行此功能来启用调试日志:
localStorage . setItem ( 'debug' , '*' )通过运行此操作:
localStorage . removeItem ( 'debug' )麻省理工学院。版权(C)Feross Aboukhadijeh和Webtorrent,LLC。