【发布时间】: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