如何在浏览器中使用网络洪流?



我对 https://github.com/feross/webtorrent#usage 中显示的示例有一些问题我正在尝试在浏览器中使用代码。所以我首先创建了一个名为app的文件.js

应用.js

var WebTorrent = require('webtorrent')
var concat = require('concat-stream')
var client = new WebTorrent()
console.log('Hi there');
client.download('magnet:?xt=urn:btih:XXXXXXXX', function (torrent) {
  // Got torrent metadata!
  console.log('Torrent info hash:', torrent.infoHash)
  torrent.files.forEach(function (file) {
    // Get the file data as a Buffer (Uint8Array typed array)
    file.createReadStream().pipe(concat(function (buf) {
      // Append a link to download the file
      var a = document.createElement('a')
      a.download = file.name
      a.href = URL.createObjectURL(new Blob([ buf ]))
      a.textContent = 'download ' + file.name
      document.body.appendChild(a)
    }))
  })
})

然后我键入命令browserify app.js > bundle.js,以便可以使代码适用于浏览器。我创建了另一个名为 index.html 的文件:

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>test</title>
    <script src="bundle.js"></script>
</head>
<body id="home">
    <h1>test</h1>

</body>
</html>

从控制台上,我只能看到"您好"。似乎 client.download() 函数不起作用。为什么会这样?我是浏览器的新手,我使用的命令有什么问题吗?

WebTorrent 只能下载明确播种到 WebTorrent 网络的种子。Torrent 客户端需要支持 WebRTC 才能与 Web 浏览器对等。目前,没有客户端支持它,但您可以使用 http://instant.io 开始播种新种子,并尝试使用应用程序中的 WebTorrent 库下载它。通过设置"localStorage.debug = '*'"来启用 http://instant.io 上的调试日志,以获取种子的信息哈希。

您还可以在此处了解更多信息:

  • 网络洪流如何工作?(https://github.com/feross/webtorrent/issues/39)
  • WebRTC BEP (https://github.com/feross/webtorrent/issues/175)

最新更新