Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot catch "Error in connection establishment" with on error event #1399

Closed
zarzen opened this issue May 20, 2018 · 4 comments
Closed

Cannot catch "Error in connection establishment" with on error event #1399

zarzen opened this issue May 20, 2018 · 4 comments

Comments

@zarzen
Copy link

@zarzen zarzen commented May 20, 2018

system info

  • webtorrent version: https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js
  • browser: chrome Version 66.0.3359.181

problem

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:

var client = new WebTorrent()
client.on('error', function(err){
    console.log('client error');
    console.log(err);
});
client.add(torrentId, function (torrent) {
  // do something to the torrent here
  ...
  // catch torrent error
  torrent.on('error', function(err){
        console.log("torrent error");
        console.log(err);
    })
})

I expect to see the error received by on-error event and logged on the console, but no. The webtorrent.min.js throw it directly in console.

@feross

This comment has been minimized.

Copy link
Member

@feross feross commented May 20, 2018

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 noPeers event:

torrent.on('noPeers', function (announceType) {})

See https://webtorrent.io/docs for more info.

@zarzen

This comment has been minimized.

Copy link
Author

@zarzen zarzen commented May 20, 2018

@feross magnet link with no peers. the link is generated by webtorrent-desktop application. If I open webtorrent-desktop for seeding, client.add() can word as expected.

@no-response no-response bot removed the need more info label May 20, 2018
@zarzen

This comment has been minimized.

Copy link
Author

@zarzen zarzen commented May 20, 2018

I stepped through my code, found it will not invoke callback function in client.add() function, where I can register noPeers event.
Here is full code:

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()
    }
  });
@SilentBot1

This comment has been minimized.

Copy link
Member

@SilentBot1 SilentBot1 commented May 20, 2018

Hey @zarzen,

The function passed through on client.add(infoHash, function) is only called once the torrents metadata is received, as stated here under the opts object. This means that since there is no peers for the torrent, and thus no way to get metadata, this function will never be called.

To get around this, you may want to not have all your logic inside the callback for client.add() by putting code which relies on having metadata such as torrent.files and file.appendTo() inside torrent.on('ready', function).

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()
  }
}
@zarzen zarzen closed this May 20, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.