From 3799d1fd97117cc98431de2ad249fc61f0fc32f9 Mon Sep 17 00:00:00 2001 From: verschmelzen Date: Tue, 22 Nov 2022 01:10:53 +0500 Subject: [PATCH] Update webrtc.js --- lib/webrtc.js | 115 +++++++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/lib/webrtc.js b/lib/webrtc.js index b746466ee..ee17fed24 100644 --- a/lib/webrtc.js +++ b/lib/webrtc.js @@ -34,19 +34,34 @@ opt.rtc.sdp = opt.rtc.sdp || {mandatory: {OfferToReceiveAudio: false, OfferToReceiveVideo: false}}; opt.rtc.max = opt.rtc.max || 55; // is this a magic number? // For Future WebRTC notes: Chrome 500 max limit, however 256 likely - FF "none", webtorrent does 55 per torrent. opt.rtc.room = opt.rtc.room || Gun.window && (location.hash.slice(1) || location.pathname.slice(1)); + var room = () => root.$.get('/RTC/'+opt.rtc.room+' msg.put['>']){ return } + room().get('+'+last).put(opt.pid, function(ack){ + if(!ack.ok || !ack.ok.rtc){ return } + open({'#': ''+ack['#'], ok: {rtc: {id: last, respect: true}}}); + }); + root.on('out', {'@': msg['#'], ok: {rtc: {id: opt.pid}}}); + }); + // TODO: implement negotiation setup here + // TODO: order greetings in this function + room().get('+'+opt.pid).on(function(last, key, msg){ + if(opt.rtc.start > msg.put['>']){ return } open({'#': ''+msg['#'], ok: {rtc: {id: last}}}); }); }; var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); root.on('create', function(at){ + console.log(opt.pid); this.to.next(at); setTimeout(opt.announce, 1); }); @@ -56,57 +71,69 @@ if(!msg.ok){ return } var rtc = msg.ok.rtc, peer, tmp; if(!rtc || !rtc.id || rtc.id === opt.pid){ return } - //console.log("webrtc:", JSON.stringify(msg)); + peer = opt.peers[rtc.id] || open[rtc.id]; + if(!peer){ + (peer = new opt.RTCPeerConnection(opt.rtc)).id = rtc.id; + var wire = peer.wire = peer.createDataChannel('dc', opt.rtc.dataChannel); + open[rtc.id] = peer; + wire.to = setTimeout(function(){delete open[rtc.id]},1000*60); + wire.onclose = function(){ mesh.bye(peer) }; + wire.onerror = function(err){ }; + wire.onopen = function(e){ + delete open[rtc.id]; + mesh.hi(peer); + } + wire.onmessage = function(msg){ + if(!msg){ return } + mesh.hear(msg.data || msg, peer); + }; + peer.onicecandidate = function(e){ // source: EasyRTC! + if(!e.candidate){ return } + // root.on('out', {'@': msg['#'], ok: {rtc: {candidate: e.candidate, id: opt.pid}}}); + root.$.get('/RTC/'+opt.rtc.room+'