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

How to stop/resume torrent ? #1004

Closed
FrancoisMentec opened this issue Dec 20, 2016 · 4 comments
Closed

How to stop/resume torrent ? #1004

FrancoisMentec opened this issue Dec 20, 2016 · 4 comments

Comments

@FrancoisMentec
Copy link

@FrancoisMentec FrancoisMentec commented Dec 20, 2016

I'm using webtorrent server side
npm : 3.10.5
webtorrent : 0.98.0

torrent.pause() only stop connecting to new peers so it doesn't stop torrent, I used : client.remove(mytorrent.infoHash)
and later :

client.add(mytorrent.magnerURI, function(torrent){
  //This callback is never triggered
});

Can't add torrent again, the torrent exists in the client but it never start, there is no metadata. I need to stop my nodejs app and restart it to add it again.

Is there an efficient way to stop/resume torrent ?

Not sure if it's the right place to ask this.

EDIT
I found a solution :
stop :

torrent.pause()
torrent.wires = [];

resume :

for(var p in torrent._peers){
  torrent.wires.push(torrent._peers[p].wire);
}
torrent.resume();

Probably not the best but it's the only way I found, what do you think about it ? is there a better way ?

@FrancoisMentec FrancoisMentec changed the title Can't stop/resume torrent How to stop/resume torrent ? Dec 21, 2016
@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Dec 27, 2016

  • Can you post the full code that you used to initially add the torrent?
  • Are you waiting until the client.add() callback is called or torrent.on('infoHash') event is fired before calling client.remove()?

I would be very surprised if client.remove() was not working.

@FrancoisMentec

This comment has been minimized.

Copy link
Author

@FrancoisMentec FrancoisMentec commented Dec 27, 2016

My function to add a torrent :

TorrentAPI.addTorrentFromMagnet = function(magnetURI){
   if(TorrentAPI.client.get(magnetURI)===null){
	    TorrentAPI.client.add(magnetURI, { path: TorrentAPI.downloadsPath }, function (torrent) {
				if(typeof torrent.infoHash == "undefined"){
					torrent.on('infoHash', function(){
						TorrentAPI.torrentsData[torrent.infoHash] = new TorrentData(torrent);
					});
				}else{
					TorrentAPI.torrentsData[torrent.infoHash] = new TorrentData(torrent);
				}
	    });
		}else{
			console.log("Torrent already added, can't duplicate it");
		}
  }

It work the first time I add the torrent but if I remove it I can't add it again. Remove work, but the callback for client.add() is only fired the first time I use it. I wait torrent.on('infoHash') to remove the torrent and use the infoHash to do it.

@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Jan 29, 2017

It looks like you're not actually removing the torrent.

The callback to client.add() is called AFTER the 'infohash' event has already fired. You need to listen to it like this:

var torrent = client.add(...)
torrent.on('infohash', onInfoHash)
torrent.on('ready,' onReady)

Note: The callback to client.add() is just a shorthand for listening to the 'ready' event.

@feross feross closed this Jan 29, 2017
@lock

This comment has been minimized.

Copy link

@lock lock bot commented May 4, 2018

This thread has been automatically locked because it has not had recent activity. To discuss futher, please open a new issue.

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 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
2 participants
You can’t perform that action at this time.