在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()函数似乎没有工作。为什么会发生这种事?我刚开始浏览,我使用的命令有什么问题吗?
发布于 2014-11-29 09:50:39
WebTorrent只能下载被显式播种到WebTorrent网络的洪流。急流客户端需要支持WebRTC来与web浏览器进行对等。目前,没有客户端支持它,但您可以使用http://instant.io开始播种一个新的洪流,并尝试使用应用程序中的WebTorrent库下载它。通过设置http://instant.io = '*‘以获取洪流的信息哈希,在`localStorage.debug上启用调试日志。
您还可以在这里了解更多信息:
https://stackoverflow.com/questions/27024705
复制相似问题