所以这是一个使用 webtorrent 的简单函数:
addTorrent (opts) {
return new Promise((resolve, reject) => {
try {
console.log('in try catch');
let torrent = wtClient.add(opts.source, (torrent) => {
console.log('client.add callback');
var nled = normalize(torrent);
console.log('normalized ', torrent.infoHash);
resolve(nled);
});
console.log('after add')
torrent.on('download', (chunkSize) => {
console.log(chunkSize);
this.emit('torrent:' + torrent.infoHash + ':download', chunkSize);
});
console.log('after on');
} catch(err) {
console.log(pe.render(err));
process.exit(1);
reject(err);
}
});
}
并称之为:
var MAGNET_URI = 'magnet:?xt=urn:btih:09e37f73e51f403bb543517f0d0a2e1283d61eb0&dn=archlinux-2015.12.01-dual.iso&tr=udp://tracker.archlinux.org:6969&tr=http://tracker.archlinux.org:6969/announce';
socketClient.connect().then(() => {
console.log('Connected to webtorrent');
return socketClient.status();
}).then((res) => {
console.log(res);
return socketClient.addTorrent({source: MAGNET_URI});
}).then((data) => {
console.log('Added torrent:', data);
}).catch((err) => {
console.log(err);
});
这是输出:
root@ubuntu-512mb-lon1-01:~/pTorrent# DEBUG=webtorrent,ut_metadata,dht,tracker,ut_pex npm start
> pTorrent@0.0.2 start /root/pTorrent
> node server.js --env=dev
webtorrent new webtorrent (peerId 2d5757303036332d323330643939666634313638, nodeId c59e8b616c66cf97437fd69f4ae037d00709b83b) +0ms
Listening at localhost:8080
Opening your system browser...
Client connected...
WEBPACK STUFF
Client connected...
in try catch
webtorrent add +42s
after add
after on
client.add callback
normalized 09e37f73e51f403bb543517f0d0a2e1283d61eb0
error occured for addTorrent
RangeError: Maximum call stack size exceeded
- Torrent.hasOwnProperty
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:17
- index.js:37 _hasBinary
[pTorrent]/[has-binary-data]/index.js:37:15
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:40
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:40
- index.js:37 _hasBinary
[pTorrent]/[has-binary-data]/index.js:37:15
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:40
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:40
- index.js:37 _hasBinary
[pTorrent]/[has-binary-data]/index.js:37:15
- index.js:47 _hasBinary
[pTorrent]/[has-binary-data]/index.js:47:40
16384
16384
16384
16384
并16384
重复,直到内存不足。
编辑:经过一番研究,事实证明 16384 是每个 x 以非常高的速率接收的字节(因为我在 VPS 上)。我的 torrent 将在几秒钟内成功下载(在 /tmp/webtorrent/ 中找到它:D)
但是,这并不能解决由resolve();
线路引起的 RangeError