UNPKG

webrtc-native

Version:
104 lines (89 loc) 2.38 kB
<html> <body> <script> function P2P(alice, bob) { alice.onicecandidate = function(event) { var candidate = event.candidate || event; bob.addIceCandidate(candidate); }; bob.onicecandidate = function(event) { var candidate = event.candidate || event; alice.addIceCandidate(candidate); }; alice.onnegotiationneeded = function(callback) { alice.createOffer(function(sdp) { alice.setLocalDescription(sdp, function() { bob.setRemoteDescription(sdp, function() { bob.createAnswer(function(sdp) { bob.setLocalDescription(sdp, function() { alice.setRemoteDescription(sdp, function() { console.log("Alice -> Bob: Connected!"); if (callback) { callback(); } }); }); }); }); }); }); }; bob.onnegotiationneeded = function(callback) { bob.createOffer(function(sdp) { bob.setLocalDescription(sdp, function() { alice.setRemoteDescription(sdp, function() { alice.createAnswer(function(sdp) { alice.setLocalDescription(sdp, function() { bob.setRemoteDescription(sdp, function() { console.log("Bob -> Alice: Connected!"); if (callback) { callback(); } }); }); }); }); }); }); }; } var config = { iceServers: [ { url: 'stun:stun.l.google.com:19302', }, ], }; var constraints = { optional: [ { DtlsSrtpKeyAgreement: true, }, ], mandatory: { OfferToReceiveAudio: true, OfferToReceiveVideo: true, }, }; console.log(window); var alice = new window.webkitRTCPeerConnection(config, constraints); var bob = new window.webkitRTCPeerConnection(config, constraints); P2P(alice, bob); alice.onnegotiationneeded(function() { alice.getStats(function(stats) { console.log(stats); stats.result().forEach(function(report) { report.names().forEach(function(name) { console.log(name); }); console.log(report); }); }); setTimeout(function() { alice.close(); bob.close(); }, 5000); }); </script> </body> </html>