Skip to content

Instantly share code, notes, and snippets.

@jasoncg
Created August 13, 2019 23:04
Show Gist options
  • Save jasoncg/51a82452d4a86fa068341f6b0fa1bd8b to your computer and use it in GitHub Desktop.
Save jasoncg/51a82452d4a86fa068341f6b0fa1bd8b to your computer and use it in GitHub Desktop.
A minimal WebRTC DataChannel example
/**
* A minimal example of a WebRTC data channel on a single host.
* After running, use dcRemote.send('message') or dcLocal.send('message') to send messages between local and remote peers
**/
let peerLocal = new RTCPeerConnection();
let peerRemote = new RTCPeerConnection();
peerLocal.onconnectionstatechange = function(e) {
console.log('peerLocal', 'onconnectionstatechange', e);
};
peerLocal.ondatachannel = function(e) {
console.log('peerLocal', 'ondatachannel', e);
};
peerLocal.onsignalingstatechange = function(e) {
console.log('peerLocal', 'onsignalingstatechange', peerLocal.signalingState);
};
peerLocal.onicegatheringstatechange = function(e) {
console.log('peerLocal', 'onicegatheringstatechange', peerLocal.iceGatheringState);
};
peerLocal.onicecandidate = function(e){
console.log('peerLocal', 'onicecandidate', e.candidate);
//Share ICE candidates with peerRemote
if(e.candidate!=null)
peerRemote.addIceCandidate(e.candidate);
};
peerLocal.onnegotiationneeded = function(e) {
console.log('peerLocal', 'onnegotiationneeded', e);
};
let dcLocal = peerLocal.createDataChannel('rtcdc');
dcLocal.onopen = function(e) {
console.log('dcLocal', 'onopen', e);
console.log('dcLocal.send("message") to send from server');
};
dcLocal.onmessage = function(e) {
console.log('dcLocal', 'onmessage', e.data);
};
dcLocal.onerror = function(e) {
console.error('dcLocal', 'onerror', e);
};
dcLocal.onclose = function(e) {
console.log('dcLocal', 'onclose', e);
};
let dcRemote = null;
peerRemote.onconnectionstatechange = function(e) {
console.log('peerRemote', 'onconnectionstatechange', e);
};
peerRemote.ondatachannel = function(e) {
// peerLocal started a data channel, so connect to it here
console.log('peerRemote', 'ondatachannel', e);
dcRemote = e.channel;
dcRemote.onopen = function(e) {
console.log('dcRemote', 'onopen', e);
console.log('dcRemote.send("message") to send from remote');
};
dcRemote.onmessage = function(e) {
console.log('dcRemote', 'onmessage', e.data);
};
dcRemote.onerror = function(e) {
console.error('dcRemote', 'onerror', e);
};
dcRemote.onclose = function(e) {
console.log('dcRemote', 'onclose', e);
};
};
peerRemote.onsignalingstatechange = function(e) {
console.log('peerRemote', 'onsignalingstatechange', peerRemote.signalingState);
};
peerRemote.onicegatheringstatechange = function(e) {
console.log('peerRemote', 'onicegatheringstatechange', peerRemote.iceGatheringState);
};
peerRemote.onicecandidate = function(e){
//Share ICE candidates with peerLocal
console.log('peerRemote', 'onicecandidate', e);
if(e.candidate!=null)
peerLocal.addIceCandidate(e.candidate);
};
peerRemote.onnegotiationneeded = function(e) {
console.log('peerRemote', 'onnegotiationneeded', e);
};
var options =null;// { offerToReceiveVideo: true, offerToReceiveAudio: true };
console.log('peerLocal', 'createOffer');
let peerLocalOffer = await peerLocal.createOffer(options);
console.log('peerLocal', 'setLocalDescription', peerLocalOffer);
await peerLocal.setLocalDescription(peerLocalOffer);
console.log('peerRemote', 'setRemoteDescription', peerLocalOffer);
await peerRemote.setRemoteDescription(peerLocalOffer);
console.log('peerRemote', 'setRemoteDescription');
let peerRemoteAnswer = await peerRemote.createAnswer();
console.log('peerRemote', 'setLocalDescription', peerRemoteAnswer);
await peerRemote.setLocalDescription(peerRemoteAnswer);
console.log('peerLocal', 'setRemoteDescription', peerRemoteAnswer);
await peerLocal.setRemoteDescription(peerRemoteAnswer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment