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 do you manually add webrtc peer? #1574

Closed
jimmywarting opened this issue Jan 27, 2019 · 1 comment
Closed

How do you manually add webrtc peer? #1574

jimmywarting opened this issue Jan 27, 2019 · 1 comment

Comments

@jimmywarting
Copy link
Contributor

@jimmywarting jimmywarting commented Jan 27, 2019

What version of WebTorrent?
0.103.0

I'm trying to connect two torrent clients within the same LAN.
To achieve this I'm using my own communication layer so no tracker, ip:port or DHT should be used.
So I want to use the WebRTC instead of TCP

In the source i found this:

webtorrent/lib/torrent.js

Lines 747 to 750 in 5b7c5ee

} else {
// `peer` is a WebRTC connection (simple-peer)
newPeer = Peer.createWebRTCPeer(peer, this)
}

I guess that is what i want to use. So to start simple I'm just trying to figure out how to do this using the same browser tab.

So here something along the line at what I'm trying to do:

const opts = { webSeeds: false, tracker: false, dht: false }
const clientA = new WebTorrent(opts)
const clientB = new WebTorrent(opts)
const file = new Blob(['abc'])

clientA.seed(file, torrentA => {
  clientB.add(torrentA.torrentFile, torrentB => {
    torrentB.addPeer(/* webrtc peer */)
  })
})

I know i also have to do the signaling myself. I think i need a bit of help with that also

It certainly would have help if there was something like torrent.createWebRTCPeer(opts) that took care of other things like adding the peer to the torrent setting a timeout, setting peerID...

@jimmywarting

This comment has been minimized.

Copy link
Contributor Author

@jimmywarting jimmywarting commented Jan 27, 2019

Figure it out by myself.
Still doe, torrent.createWebRTCPeer(opts) would be useful maybe

For any references, this is what i did to connect all tabs to eachother
needs better work to handle adding to correct torrent (infoHash)

let bc = new BroadcastChannel('foo1')

function onTorrent (torrent) {
      // Adding myself to the mix
      let initiator = true
      let me = new simplePeer({ initiator })
      let peerId = client.peerId
      me.id = peerId
      torrent.addPeer(me)

      bc.onmessage = evt => {
        var { sdp, initiator, peerId } = evt.data
        if (initiator) {
          let otherPeer = new WebTorrent.simplePeer()
          otherPeer.id = peerId
          torrent.addPeer(otherPeer)
          otherPeer.signal(sdp)
          otherPeer.on('signal', sdp => {
            bc.postMessage({ sdp, initiator: false, peerId })
          })
        } else if (sdp.type === 'answer') {
          torrent._peers[peerId].conn.signal(sdp)
        }
      }

      me.on('signal', sdp => {
        bc.postMessage({ sdp, initiator, peerId })
        initiator = false
      })
}
@lock lock bot locked as resolved and limited conversation to collaborators Apr 27, 2019
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
1 participant
You can’t perform that action at this time.