Created
March 19, 2018 20:48
-
-
Save eladg/355cebf096a4857650132d0a30270673 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var simplexPeer = new function() { | |
// const | |
this.ICE_SERVERS = [{ | |
urls: "stun:stun.l.google.com:19302", | |
} | |
] | |
this.CHANNEL_KEY = 'webrtc/channel'; | |
this.SENDER_KEY = 'webrtc/sender-description'; | |
this.RECEIVER_KEY = 'webrtc/receiver-description'; | |
// memebers | |
this.peer_id = null; | |
this.socket = null; | |
this.peer = null; | |
this.channel = null; | |
this.session = null; | |
this.type = null; | |
// utils | |
this.trace = function(msg) { | |
var now = (window.performance.now() / 1000).toFixed(3); | |
console.log(now + ': ', msg); | |
} | |
this.recordKey = function() { | |
return "webrtc/" + this.peer_id + "-" + this.type; | |
} | |
this.socketConnect = function(wssUrl) { | |
var _this = this; | |
this.socket = deepstream( wssUrl ); | |
this.socket.login(); | |
// remote description callback | |
var remoteDesc = this.socket.record.getRecord( this.peer_id + "-" + this.RECEIVER_KEY ); | |
remoteDesc.subscribe( function() { | |
// if we are the sender... | |
if (_this.type === "sender") { | |
console.log("sender: setting remote"); | |
_this.peer.setRemoteDescription(remoteDesc.get()); | |
} | |
// if we are the receiver | |
if (_this.type === "receiver") { | |
console.log("receiver: setting local"); | |
_this.peer.setLocalDescription(remoteDesc.get()); | |
} | |
}); | |
// local description callback | |
var localDesc = this.socket.record.getRecord( this.peer_id + "-" + this.SENDER_KEY ); | |
localDesc.subscribe( function() { | |
// if we are the sender... | |
if (_this.type === "sender") { | |
console.log("sender: setting local"); | |
_this.peer.setLocalDescription(localDesc.get()); | |
} | |
// if we are the receiver | |
if (_this.type === "receiver") { | |
console.log("receiver: setting remote"); | |
_this.peer.setRemoteDescription(localDesc.get()); | |
} | |
}); | |
} | |
this.peerConnect = function(name) { | |
var _this = this; | |
var pcConstraint = null; | |
_this.peer = new RTCPeerConnection( { iceServers: _this.ICE_SERVERS }, pcConstraint); | |
_this.trace('new peerConnection.'); | |
// ICE Candidate callbacks | |
var candidateSuccessful = function() { | |
_this.trace("ICE added successfully") | |
}; | |
var candidateError = function(err) { | |
// _this.trace("failed to add ICE candidate: "); | |
// _this.trace(err); | |
}; | |
_this.peer.onicecandidate = function(event) { | |
_this.peer.addIceCandidate(event.candidate) | |
.then( | |
function() { | |
candidateSuccessful(); | |
}, | |
function(err) { | |
candidateError(err); | |
} | |
); | |
// trace('ICE candidate: \n' + (event.candidate ? event.candidate.candidate : '(null)')); | |
} | |
// webrtc data channel | |
var channelConstraint = null; | |
_this.channel = _this.peer.createDataChannel(name, channelConstraint); | |
var channelStateChange = function(arg1) { | |
_this.trace("on channelStateChange: "); | |
_this.trace("got: ", arg1); | |
} | |
_this.channel.onopen = channelStateChange; | |
_this.channel.onclose = channelStateChange; | |
} | |
this.createOfferCallback = function(_this, desc) { | |
var record = _this.socket.record.getRecord( this.peer_id + "-" + this.SENDER_KEY ); | |
record.set({ | |
id: _this.peer_id, | |
type: desc.type, | |
sdp: desc.sdp, | |
}); | |
} | |
this.createOfferError = function(err) { | |
this.trace("createOfferError"); | |
this.trace(err); | |
} | |
this.createAnswerCallback = function(_this, desc) { | |
var record = _this.socket.record.getRecord( this.peer_id + "-" + this.RECEIVER_KEY ); | |
record.set({ | |
id: _this.peer_id, | |
type: desc.type, | |
sdp: desc.sdp, | |
}); | |
} | |
this.createAnswerError = function(_this, err) { | |
_this.trace("createAnswerError"); | |
} | |
this.connect = function(name, wss, type, callback) { | |
var _this = this; | |
this.peer_id = Math.random().toString(36).substring(7); | |
this.type = type; | |
this.peerConnect(name); | |
this.socketConnect(wss); | |
if (type === "sender") { | |
this.peer.createOffer().then( | |
function(desc) { | |
_this.createOfferCallback(_this, desc); | |
}, | |
function(err) { | |
_this.createOfferError(_this, err); | |
} | |
); | |
} | |
if (type === "receiver") { | |
var offerDesc = this.socket.record.getRecord( this.peer_id + "-" + this.SENDER_KEY ).get(); | |
if (offerDesc.type === 'offer') { | |
this.peer.createAnswer().then( | |
function(desc) { | |
_this.createAnswerCallback(_this, desc); | |
}, | |
function(err) { | |
_this.createAnswerError(_this, err); | |
} | |
); | |
} | |
} | |
}; | |
// =========================================== | |
// == Public API | |
// =========================================== | |
this.connectSender = function(name, wss, callback) { | |
this.connect(name, wss, "sender", callback); | |
} | |
this.connectReceiver = function(name, wss, callback) { | |
this.connect(name, wss, "receiver", callback); | |
} | |
} | |
export default simplexPeer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment