Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upCannot catch "Error in connection establishment" with on error event #1399
Comments
This comment has been minimized.
This comment has been minimized.
|
Are you adding a completely invalid magnet link? Or just a magnet link with no peers? A magnet link with no peers should not cause an error. Instead, it should emit a torrent.on('noPeers', function (announceType) {})See https://webtorrent.io/docs for more info. |
This comment has been minimized.
This comment has been minimized.
|
@feross magnet link with no peers. the link is generated by |
This comment has been minimized.
This comment has been minimized.
|
I stepped through my code, found it will not invoke callback function in function startVideo(torrentId) {
// torrentId is a magnet link with no peers
var client = new WebTorrent()
// HTML elements
var $body = document.body
var $progressBar = document.querySelector('#progressBar')
var $numPeers = document.querySelector('#numPeers')
var $downloaded = document.querySelector('#downloaded')
var $total = document.querySelector('#total')
var $remaining = document.querySelector('#remaining')
var $uploadSpeed = document.querySelector('#uploadSpeed')
var $downloadSpeed = document.querySelector('#downloadSpeed')
client.on('error', function(err){
console.log('client error');
console.log(err);
});
// Download the torrent
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
return file.name.endsWith('.mp4')
})
// Stream the file in the browser
file.appendTo('#output')
// Trigger statistics refresh
torrent.on('done', onDone)
torrent.on('error', function(err){
console.log("torrent error");
console.log(err);
})
torrent.on('noPeers', function (announceType){
console.log(announceType);
})
setInterval(onProgress, 500)
onProgress()
// Statistics
function onProgress() {
// Peers
$numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers')
// Progress
var percent = Math.round(torrent.progress * 100 * 100) / 100
$progressBar.style.width = percent + '%'
$downloaded.innerHTML = prettyBytes(torrent.downloaded)
$total.innerHTML = prettyBytes(torrent.length)
// Remaining time
var remaining
if (torrent.done) {
remaining = 'Done.'
} else {
remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.'
}
$remaining.innerHTML = remaining
// Speed rates
$downloadSpeed.innerHTML = prettyBytes(torrent.downloadSpeed) + '/s'
$uploadSpeed.innerHTML = prettyBytes(torrent.uploadSpeed) + '/s'
}
function onDone() {
$body.className += ' is-seed'
onProgress()
}
}); |
This comment has been minimized.
This comment has been minimized.
|
Hey @zarzen, The function passed through on To get around this, you may want to not have all your logic inside the callback for This is what the modifications could look like: function startVideo(torrentId) {
// torrentId is a magnet link with no peers
var client = new WebTorrent()
// HTML elements
var $body = document.body
var $progressBar = document.querySelector('#progressBar')
var $numPeers = document.querySelector('#numPeers')
var $downloaded = document.querySelector('#downloaded')
var $total = document.querySelector('#total')
var $remaining = document.querySelector('#remaining')
var $uploadSpeed = document.querySelector('#uploadSpeed')
var $downloadSpeed = document.querySelector('#downloadSpeed')
client.on('error', function(err){
console.log('client error');
console.log(err);
});
// Download the torrent
var torrent = client.add(torrentId)
torrent.on('ready', function(){
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
return file.name.endsWith('.mp4')
})
// Stream the file in the browser
file.appendTo('#output')
})
// Trigger statistics refresh
torrent.on('done', onDone)
torrent.on('error', function(err){
console.log("torrent error");
console.log(err);
})
torrent.on('noPeers', function (announceType){
console.log(announceType);
})
setInterval(onProgress, 500)
onProgress()
// Statistics
function onProgress() {
// Peers
$numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers')
// Progress
var percent = Math.round(torrent.progress * 100 * 100) / 100
$progressBar.style.width = percent + '%'
$downloaded.innerHTML = prettyBytes(torrent.downloaded)
$total.innerHTML = prettyBytes(torrent.length)
// Remaining time
var remaining
if (torrent.done) {
remaining = 'Done.'
} else {
remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.'
}
$remaining.innerHTML = remaining
// Speed rates
$downloadSpeed.innerHTML = prettyBytes(torrent.downloadSpeed) + '/s'
$uploadSpeed.innerHTML = prettyBytes(torrent.uploadSpeed) + '/s'
}
function onDone() {
$body.className += ' is-seed'
onProgress()
}
} |
system info
https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.jsproblem
I try to use
client.add()adding a magnet link that no one is seeding. I want to simulate such corner case and want to catch the error.I tried following code snippet:
I expect to see the error received by on-error event and logged on the console, but no. The
webtorrent.min.jsthrow it directly in console.