diff --git a/index.js b/index.js index 332655f..867f95f 100644 --- a/index.js +++ b/index.js @@ -184,15 +184,14 @@ module.exports = class WebPeer extends ReadyResource { function waitForRemote (remote) { return new Promise(resolve => { - this.remote.on('open', done) - this.remote.on('open', done) - this.remote.on('error', done) - this.remote.on('close', done) + remote.on('open', done) + remote.on('error', done) + remote.on('close', done) function done () { - this.remote.off('open', done) - this.remote.off('error', done) - this.remote.off('close', done) + remote.off('open', done) + remote.off('error', done) + remote.off('close', done) resolve() } diff --git a/index2.js b/index2.js deleted file mode 100644 index 7f63bcb..0000000 --- a/index2.js +++ /dev/null @@ -1,80 +0,0 @@ -const iceServers = [ - { urls: 'stun:stun.l.google.com:19302' } -] - -let $id = 0 - -module.exports = class LikeWebRTC { - constructor () { - this._id = $id++ - - this.peer = new RTCPeerConnection({ iceServers }) - - this._setup() - } - - _setup () { - this.peer.onicecandidate = (event) => { - if (event.candidate) { - console.log(this._id, 'onicecandidate', event) - this.onice(event) - } - } - } - - addIceCandidate (candidate) { - this.peer.addIceCandidate(new RTCIceCandidate(candidate)) - } - - onice (event) {} - - // - - // connect () {} - - // - - createChannel (name) { - const channel = this.peer.createDataChannel(name) - // const channel = this.peer.createDataChannel(name, { negotiated: true, id: 0 }) // ondatachannel will not fire - - channel.onopen = () => { - console.log('Data channel opened'); - channel.send('Hello, peer!'); - }; - - channel.onclose = () => { - console.log('data channel closed') - } - - channel.onerror = (err) => console.error(err) - - channel.onmessage = (event) => { - console.log('Received message:', event.data); - }; - - return channel - } - - async createOffer () { - const offer = await this.peer.createOffer() - - await this.peer.setLocalDescription(offer) - - return this.peer.localDescription // => offer - } - - async receiveOffer (offer) { - this.peer.setRemoteDescription(new RTCSessionDescription(offer)) - - const answer = await this.peer.createAnswer() - - await this.peer.setLocalDescription(answer) - - return this.peer.localDescription // => answer - } - - async receiveAnswer (answer) { - this.peer.setRemoteDescription(new RTCSessionDescription(answer)) - } -} diff --git a/test2.js b/test2.js deleted file mode 100644 index d960702..0000000 --- a/test2.js +++ /dev/null @@ -1,40 +0,0 @@ -const test = require('brittle') -const WebRTC = require('./index2.js') - -test('basic', async function (t) { - const a = new WebRTC() - const b = new WebRTC() - - a.onice = function (e) { - b.addIceCandidate(e.candidate) - } - - b.onice = function (e) { - a.addIceCandidate(e.candidate) - } - - const channel1 = a.createChannel('rohil-is-he-knows') - b.peer.ondatachannel = function (e) { - const channel2 = e.channel - - channel2.onopen = () => { - console.log('Data channel opened'); - }; - - channel2.onclose = () => { - console.log('data channel closed') - } - - channel2.onerror = (err) => console.error(err) - - channel2.onmessage = (event) => { - console.log('Received message:', event.data); - }; - } - - const offer = await a.createOffer() - const answer = await b.receiveOffer(offer) - await a.receiveAnswer(answer) - - -})