Created
June 27, 2017 05:27
-
-
Save louislang/123f4692554a844b1a09a6f7be1c8a51 to your computer and use it in GitHub Desktop.
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
//https://stackoverflow.com/questions/18833920/webrtc-for-realtime-scaling/21103641#21103641 | |
/* | |
* Client 1 Server Client 2 | |
* Create an RTC object | |
* Create offer | |
* set localdescription = offer Create RTC object | |
* send offer -------------------> ------>set remote description to offer | |
* Create answer | |
* local description = answer | |
* set remote description<-------- <------send answer | |
* | |
* This goes both directions: | |
* onicecandidate send ----------> ------>set ICE candidate | |
* | |
* Connection done! | |
*/ | |
// | |
var dc; | |
// Create a new connection to our signalling server. | |
var serverConnection = new WebSocket('ws://127.0.0.1:3434'); | |
/** | |
* Process any messages we receive from the signalling server. | |
*/ | |
serverConnection.onmessage = function(message) { | |
console.log('Received message from the server'); | |
var signal = JSON.parse(message.data); | |
if(signal.sdp) { | |
console.log('Server sent SDP signal'); | |
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() { | |
if(signal.sdp.type == 'offer') { | |
peerConnection.createAnswer(descriptionHandler, createAnswerError); | |
} | |
}); | |
} else if(signal.ice) { | |
console.log('Server sent ICE signal'); | |
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)); | |
} | |
} | |
// Setup our peer config and create a new peer connection. | |
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]}; | |
var options = { | |
optional: [ | |
{DtlsSrtpKeyAgreement: true}, | |
{RtpDataChannels: true} //required for Firefox | |
] | |
} | |
var peerConnection = new RTCPeerConnection(peerConnectionConfig, options); | |
/** | |
* When we receive the ICE candidate. | |
*/ | |
peerConnection.onicecandidate = function(event) { | |
console.log('Received ICE candidate:', event); | |
if(event.candidate != null) { | |
serverConnection.send(JSON.stringify({ 'ice': event.candidate })); | |
} | |
} | |
/** | |
* Create the local offer. | |
*/ | |
function createOffer() { | |
// Create our data channel | |
dc = peerConnection.createDataChannel("messages"); | |
dc.onmessage = function (event) { | |
console.log("received: " + event.data); | |
}; | |
dc.onopen = function () { | |
console.log("datachannel open"); | |
var readyState = dc.readyState; | |
if (readyState == "open") { | |
dc.send("Hello"); | |
} | |
}; | |
dc.onclose = function () { | |
console.log("datachannel close"); | |
}; | |
peerConnection.createOffer(descriptionHandler, createOfferErrorHandler); | |
} | |
/** | |
* Handles description. | |
*/ | |
function descriptionHandler(description) { | |
peerConnection.setLocalDescription(description, function() { | |
console.log('Sending description to the server'); | |
serverConnection.send(JSON.stringify({ 'sdp': description })); | |
}, function() { console.log('Set description error') }); | |
} | |
/** | |
* | |
*/ | |
function createOfferErrorHandler(error) { | |
console.log(error); | |
} | |
/** | |
* | |
*/ | |
function createAnswerError(error) { | |
console.log('createAnswer() error: ', e); | |
} | |
/** | |
* | |
*/ | |
function sendMsg() { | |
dc.send('Hello!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment