實例化與參數設定
var engine = new P2PEngineFile(url, p2pConfig);
建立一個新的 P2PEngineFile 實例,其中 url 是檔案下載位址。如果指定了 p2pConfig,那麼對應的預設值將會被覆蓋。
| 欄位 | 型別 | 預設值 | 描述 |
|---|---|---|---|
| logLevel | string|boolean | 'error' | log的等級,分為warn、error、none,設為true等於warn,設為false等於none。 |
| token | string | undefined | token用於控制台多網域資料彙總展示,另外如果自訂channelId也需要設定token。 |
| trackerZone | string | 'eu' | tracker伺服器位址的國家代號,分為'cn'、'eu'、'hk'、'us'。 |
| p2pEnabled | boolean | true | 是否開啟P2P。 |
| webRTCConfig | Object | {} | 用於設定stun和datachannel的字典。 |
| pieceLength | number | 1024 * 1024 | 每個切片的大小(單位:Byte),相同pieceLength的節點才能互相連線。 |
| httpMaxRetrys | number | 2 | HTTP下載某個切片的最大重試次數。 |
| mitm | string | 'https://cdnbye.github.io/file-p2p-engine/demo/mitm.html' | 用於安裝ServiceWorker並觸發瀏覽器預設檔案下載行為的iFrame位址,預設託管在github。 |
| diskCacheLimit | Object | {"pc": 1500 * 1024 * 1024, "mobile": 1000 * 1024 * 1024} | 磁碟快取的最大資料量,分為PC和mobile。 |
P2PEngineFile API
P2PEngineFile.version (static)
取得 P2PEngineFile 的版本號。
P2PEngineFile.protocolVersion (static)
取得 P2P 協定的版本號。
P2PEngineFile.isSupported() (static method)
判斷目前瀏覽器是否同時支援WebRTC datachannel。
engine.start()
開始檔案下載。
engine.pause()
暫停檔案下載。
engine.resume()
繼續檔案下載。
engine.stop()
停止檔案下載、關閉P2P並銷毀engine釋放記憶體。
engine.destroy()
停止檔案下載、關閉P2P並銷毀engine釋放記憶體。
engine.isSaveByStreamSupported
檢查瀏覽器是否支援基於ServiceWorker的串流下載。
engine.saveByServiceWorkerStream(filename)
觸發瀏覽器預設檔案下載行為。
P2PEngineFile 事件
engine.on('metadata', function (source) {})
取得檔案的中繼資訊,包括:
source.getUrl():檔案下載位址
source.getMime():檔案的 MimeType
source.getFileLength():檔案大小
engine.on('progress', function (ratio) {})
檔案下載進度。
engine.on('failed', function () {})
檔案下載失敗,監聽到該回呼後可以回退到普通下載模式。
engine.on('speed', function (speed) {})
平均下載速度(byte/s)。
engine.on('finished', function (file) {})
檔案下載完成後回呼。file 是一個檔案物件,用於對檔案進行操作,包括:
file.save(fileName):儲存檔案到本地,fileName是儲存的檔名
file.getBlobURL():取得 BlobURL
file.revokeBlobURL():撤銷對 BlobURL 的引用,釋放記憶體
engine.on('peerId', function (peerId) {})
當從伺服器端取得peerId時回呼該事件。
engine.on('peers', function (peers) {})
當與新的節點成功建立p2p連線時回呼該事件。
engine.on('stats', function (stats) {})
該回呼函式可以取得p2p資訊,包括:
stats.totalHTTPDownloaded: 從HTTP(CDN)下載的資料量(單位KB)
stats.totalP2PDownloaded: 從P2P下載的資料量(單位KB)
stats.totalP2PUploaded: P2P上傳的資料量(單位KB)
stats.p2pDownloadSpeed: P2P下載速度(單位KB/s)
engine.on('serverConnected', function (connected) {})
當連線/中斷websocket時回呼該事件。
engine.on('exception', function (e) {})
該回呼函式可以取得SDK的異常資訊,包括:
e.code: 異常識別碼(TRACKER_EXPT SIGNAL_EXPT)
e.message: 異常資訊
e.stack: 異常堆疊資訊
透過p2pConfig取得p2p資訊
p2pConfig: {
getStats: function (totalP2PDownloaded, totalP2PUploaded, totalHTTPDownloaded, p2pDownloadSpeed) {
// 取得p2p下載資訊
},
getPeerId: function (peerId) {
// 取得本節點的Id
},
getPeersInfo: function (peers) {
// 取得成功連線的節點的資訊
}
}進階用法
解決動態檔案路徑問題
某些檔案位址是動態產生的,不同節點的檔案位址不一樣,例如example.com/clientId1/fileId.file和example.com/clientId2/fileId.file, 而本外掛程式預設使用檔案作為channelId。這時候就要建構一個共同的channelId,使實際下載同一檔案的節點處在相同頻道中。
// 必須先在 p2pConfig 設定 token,才能自訂 channelId !
p2pConfig: {
token: YOUR_TOKEN,
channelId: function (url) {
const fileId = extractFileIdFromUrl(url); // 忽略差異部分,建構一個一致的channelId,其中 extractFileIdFromUrl 需要自己定義,可以擷取url中的檔案ID作為結果回傳
return fileId;
}
// channelId: VIDEO_ID // for fixed channel id
}