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/2db69e020ca9381301466f60cfffbc33 to your computer and use it in GitHub Desktop.

Select an option

Save yinxin630/2db69e020ca9381301466f60cfffbc33 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>
<input type="file" />
<button id="send">发送</button>
<script>
console.log('start');
const sendConnection = new RTCPeerConnection();
const sendChannel = sendConnection.createDataChannel('sendChannel');
sendChannel.addEventListener('open', () => {
console.log('== open ==', sendChannel.readyState);
console.log('send data');
sendChannel.send(new Date());
sendConnection.close();
});
window.setCandidate = (candidate) => {
candidate = new RTCIceCandidate(JSON.parse(candidate));
console.log(candidate);
sendConnection.addIceCandidate(candidate);
};
window.setAnswer = async (sdp) => {
const answer = new RTCSessionDescription({
sdp,
type: 'answer',
});
await sendConnection.setRemoteDescription(answer);
};
async function init() {
const offer = await sendConnection.createOffer();
console.log('offer', JSON.stringify(offer.sdp));
await sendConnection.setLocalDescription(offer);
}
init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment