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。