Last active
August 16, 2019 09:03
-
-
Save yinxin630/2db69e020ca9381301466f60cfffbc33 to your computer and use it in GitHub Desktop.
|-|&tag=code
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
| <!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