首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在浏览器中使用webtorrent?

如何在浏览器中使用webtorrent?
EN

Stack Overflow用户
提问于 2014-11-19 18:54:41
回答 1查看 9.5K关注 0票数 8

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

app.js

代码语言:javascript
复制
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

代码语言:javascript
复制
<!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()函数似乎没有工作。为什么会发生这种事?我刚开始浏览,我使用的命令有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-29 09:50:39

WebTorrent只能下载被显式播种到WebTorrent网络的洪流。急流客户端需要支持WebRTC来与web浏览器进行对等。目前,没有客户端支持它,但您可以使用http://instant.io开始播种一个新的洪流,并尝试使用应用程序中的WebTorrent库下载它。通过设置http://instant.io = '*‘以获取洪流的信息哈希,在`localStorage.debug上启用调试日志。

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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27024705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档