所以这里有一个使用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
经过一番研究,发现16384是每x以非常高的速率接收的字节(因为我在VPS上)。我的torrent将在几秒钟内成功下载(在/tmp/webtorrent/
:D中找到)
RangeError是由我的normalize函数引起的,该函数会留下对象并使其过大。