Skip to content

Instantly share code, notes, and snippets.

@tegila
Last active May 21, 2023 01:52
Show Gist options
  • Save tegila/b70d2119c74eb84edf1f2bca30d242d5 to your computer and use it in GitHub Desktop.
Save tegila/b70d2119c74eb84edf1f2bca30d242d5 to your computer and use it in GitHub Desktop.
const log = console.log;
(async () => {
const localConnection = new RTCPeerConnection();
log(localConnection);
const lsendChannel = localConnection.createDataChannel("sendChannel");
const remoteConnection = new RTCPeerConnection();
log(remoteConnection);
const rsendChannel = remoteConnection.createDataChannel("sendChannel");
localConnection.onicecandidate = (e) =>
!e.candidate || remoteConnection.addIceCandidate(e.candidate).catch(log);
remoteConnection.onicecandidate = (e) =>
!e.candidate || localConnection.addIceCandidate(e.candidate).catch(log);
// offer
const offer = await localConnection.createOffer();
log(offer);
await localConnection.setLocalDescription(offer);
// answer
await remoteConnection.setRemoteDescription(localConnection.localDescription);
const answer = await remoteConnection.createAnswer();
await remoteConnection.setLocalDescription(answer);
// finish
await localConnection.setRemoteDescription(remoteConnection.localDescription);
log(remoteConnection);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment