Last active
April 29, 2020 13:52
-
-
Save BranLiang/b7645cf490dbe54742b3617bbc6b85fa 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
import { nanoid } from 'nanoid' | |
class Connection { | |
peerConnection | |
channel | |
constructor() { | |
this.identifier = nanoid() | |
this.localICECandidates = [] | |
this.connected = false | |
} | |
createPeerConnection(servers) { | |
this.peerConnection = new RTCPeerConnection({ | |
iceServers: servers | |
}); | |
this.peerConnection.ontrack = ({track, streams}) => { | |
console.log("<<< Received new track") | |
} | |
this.peerConnection.onicecandidate = ({candidate}) => { | |
if (candidate) { | |
console.log(`<<< Received local ICE candidate from STUN/TURN server (${candidate.address})`) | |
if (this.connected) { | |
console.log(`>>> Sending local ICE candidate (${candidate.address})`) | |
this.channel.send({ | |
type: "CANDIDATE", | |
name: this.identifier, | |
sdp: JSON.stringify(candidate) | |
}) | |
} else { | |
console.log(`>>> Buffer local candidate (${candidate.address})`) | |
this.localICECandidates.push(candidate) | |
} | |
} | |
} | |
} | |
createOffer() { | |
let that = this; | |
this.peerConnection.createOffer( | |
function(offer) { | |
console.log(">>> Sending offer to receivers") | |
that.peerConnection.setLocalDescription(offer) | |
that.channel.send({ | |
type: "OFFER", | |
name: that.identifier, | |
sdp: JSON.stringify(offer) | |
}) | |
} | |
) | |
} | |
createAnswer(offer) { | |
console.log("<<< Answering to caller") | |
this.connected = true | |
let rtcOffer = new RTCSessionDescription(offer); | |
let that = this | |
this.peerConnection.setRemoteDescription(rtcOffer); | |
this.loadStream() | |
this.peerConnection.createAnswer( | |
function(answer) { | |
that.peerConnection.setLocalDescription(answer) | |
that.channel.send({ | |
type: "ANSWER", | |
name: that.identifier, | |
sdp: JSON.stringify(answer) | |
}) | |
} | |
) | |
} | |
receiveAnswer(answer) { | |
console.log(">>> Receive remote answer") | |
let rtcAnswer = new RTCSessionDescription(answer); | |
let that = this | |
this.peerConnection.setRemoteDescription(rtcAnswer) | |
this.connected = true | |
this.localICECandidates.forEach(candidate => { | |
console.log(`>>> Sending local ICE candidate (${candidate.address})`) | |
this.channel.send({ | |
type: "CANDIDATE", | |
name: this.identifier, | |
sdp: JSON.stringify(candidate) | |
}) | |
}) | |
this.localICECandidates = [] | |
} | |
addCandidate(candidate) { | |
let rtcCandidate = new RTCIceCandidate(candidate); | |
console.log(`<<< Adding ICE candidate (${rtcCandidate.address} - ${rtcCandidate.relatedAddress})`) | |
this.peerConnection.addIceCandidate(rtcCandidate) | |
} | |
} | |
export default Connection; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment