【问题标题】:How to use webtorrent in browser?如何在浏览器中使用 webtorrent?
【发布时间】:2015-01-17 10:26:43
【问题描述】:

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

app.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 的文件:

index.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() 函数不起作用。为什么会这样?我是browserify的新手,我使用的命令有什么问题吗?

【问题讨论】:

    标签: javascript html browserify webtorrent


    【解决方案1】:

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

    您还可以在这里了解更多信息:

    【讨论】:

      猜你喜欢
      • 2018-04-10
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      相关资源
      最近更新 更多