Skip to content

Instantly share code, notes, and snippets.

@yinxin630
Last active August 16, 2019 09:03
Show Gist options
  • Select an option

  • Save yinxin630/29da61f1f447551adce868609fc11351 to your computer and use it in GitHub Desktop.

Select an option

Save yinxin630/29da61f1f447551adce868609fc11351 to your computer and use it in GitHub Desktop.
|-|&tag=code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>webrtc</title>
</head>
<body>
<script>
console.log('start');
const reveiveConnection = new RTCPeerConnection();
reveiveConnection.addEventListener('icecandidate', (event) => {
console.log('-- receiver icecandidate --', JSON.stringify(event.candidate));
});
reveiveConnection.addEventListener('datachannel', (event) => {
console.log('-- datachannel --', event.channel);
event.channel.onmessage = ({ data }) => {
console.log('receive data', data);
reveiveConnection.close();
};
});
window.setOffer = async (sdp) => {
const offer = new RTCSessionDescription({
sdp,
type: 'offer',
});
await reveiveConnection.setRemoteDescription(offer);
const answer = await reveiveConnection.createAnswer();
console.log('answer', JSON.stringify(answer.sdp));
await reveiveConnection.setLocalDescription(answer);
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment