answerScript.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. (function (){
  2. console.log('Hello');
  3. const connectRemote = document.getElementById('connectRemote')
  4. connectRemote.addEventListener('click', createRemoteConnection, false)
  5. })();
  6. function createRemoteConnection() {
  7. const remoteDescText = document.getElementById('remoteDesc').value
  8. const remoteDescJSON = JSON.parse(remoteDescText);
  9. const remoteDesc = {
  10. type: "offer",
  11. sdp: remoteDescJSON.description
  12. }
  13. const remoteCandidate = {
  14. candidate: remoteDescJSON.candidate,
  15. sdpMid: "0", // Media stream ID for audio
  16. sdpMLineIndex: 0 // Something to do with media
  17. }
  18. const remoteConnection = new RTCPeerConnection();
  19. const connectionInfo = {};
  20. remoteConnection.setRemoteDescription(remoteDesc).then((e) => {
  21. console.log(e)
  22. });
  23. remoteConnection.addIceCandidate(remoteCandidate).then(
  24. () => {console.log('yes')}
  25. )
  26. remoteConnection.createAnswer().then((desc) => {
  27. remoteConnection.setLocalDescription(desc);
  28. console.dir(desc);
  29. connectionInfo.description = desc.sdp;
  30. })
  31. remoteConnection.onicecandidate = e => {
  32. if (e.candidate) {
  33. const candidate = e.candidate.candidate;
  34. connectionInfo.candidate = candidate;
  35. document.body.append(JSON.stringify(connectionInfo));
  36. }
  37. }
  38. remoteConnection.onicecandidateerror = err => {
  39. console.log(err)
  40. }
  41. remoteConnection.ondatachannel = (e) => {
  42. console.log('onDataChannel')
  43. const receiveChannel = e.channel;
  44. console.dir(receiveChannel);
  45. receiveChannel.onmessage = (msg) => {
  46. document.body.append(msg.data);
  47. }
  48. }
  49. }