Created
May 22, 2017 18:02
-
-
Save oxygen/9eca0fa5046d4c5ef7dcb13ac1047de6 to your computer and use it in GitHub Desktop.
WebRTC short localhost example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makePeerConnection() | |
{ | |
var myPeerConnection = new RTCPeerConnection( | |
{ | |
iceServers: [ | |
/*{ | |
urls: "turn:192.168.137.3:3478", | |
username: "guest", | |
credential: "*********" | |
}*/ | |
] | |
}, | |
{ | |
'mandatory': { | |
'OfferToReceiveAudio': false, | |
'OfferToReceiveVideo': false | |
} | |
} | |
); | |
return myPeerConnection; | |
} | |
(async ()=>{ | |
const connectionA=makePeerConnection(); | |
const connectionB=makePeerConnection(); | |
let dataChannelB; | |
const dataChannelA = connectionA.createDataChannel("jsonrpc"); | |
dataChannelA.onmessage = (event) => { | |
console.log(event.data); | |
dataChannelA.send(JSON.stringify({result:1234,jsonrpc:"2.0",id:null})); | |
}; | |
dataChannelA.onerror = console.error; | |
connectionA.onicecandidate = (event) => { | |
connectionB.addIceCandidate(event.candidate).catch(()=>{}); | |
}; | |
connectionB.onicecandidate = (event) => { | |
connectionA.addIceCandidate(event.candidate).catch(()=>{}); | |
}; | |
connectionB.ondatachannel = (event) => { | |
dataChannelB = event.channel; | |
dataChannelB.onmessage = (event) => {console.log(event.data)}; | |
dataChannelB.send(JSON.stringify({method:"arici",params:[],jsonrpc:"2.0",id:null})); | |
}; | |
const offer = await connectionA.createOffer(); | |
await connectionA.setLocalDescription(new RTCSessionDescription(offer)); | |
await connectionB.setRemoteDescription(new RTCSessionDescription(offer)); | |
const answer = await connectionB.createAnswer(); | |
await connectionB.setLocalDescription(answer); | |
await connectionA.setRemoteDescription(answer); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment