answerScript.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function (){
  2. const connectionParam = getQueryParameter('connection');
  3. const parsedConnection = atob(connectionParam).split(',');
  4. const connectionMetadata = {
  5. description: parsedConnection[0],
  6. candidate: parsedConnection[1]
  7. };
  8. createRemoteConnection(connectionMetadata);
  9. })();
  10. function createRemoteConnection(connectionMetadata) {
  11. console.log(connectionMetadata);
  12. const connection = new RTCPeerConnection();
  13. const remoteDesc = {
  14. type: 'offer',
  15. sdp: connectionMetadata.description
  16. };
  17. connection.setRemoteDescription(remoteDesc)
  18. .then(() => UI.setConnectionState('set remote description'));
  19. let connectionDescription = '';
  20. const remoteCandidate = {
  21. candidate: connectionMetadata.candidate,
  22. sdpMid: '0', // Media stream ID for audio
  23. sdpMLineIndex: 0 // Something to do with media
  24. };
  25. connection.addIceCandidate(remoteCandidate).then(
  26. () => UI.setConnectionState('candidate added')
  27. );
  28. connection.createAnswer().then((desc) => {
  29. connection.setLocalDescription(desc);
  30. connectionDescription = desc.sdp;
  31. });
  32. connection.onicecandidate = e => {
  33. if (e.candidate) {
  34. const body = connectionDescription;
  35. fetch(
  36. 'http://localhost:8000/state/json',
  37. {
  38. method: 'post',
  39. headers: {'Content-Type': 'text/plain'},
  40. body
  41. }
  42. ).then(response => {
  43. if (response.status !== 200) {
  44. throw new Error('bad status ' + response.status);
  45. }
  46. });
  47. }
  48. };
  49. connection.ondatachannel = (e) => {
  50. UI.setConnectionState('received channel');
  51. const channel = e.channel;
  52. console.dir(channel);
  53. const sendButton = document.getElementById('sendDataBtn');
  54. sendButton.addEventListener('click', sendMessage.bind(null, channel), false);
  55. channel.onopen = () => {
  56. console.log('channel open');
  57. UI.setConnectionState('channel open');
  58. };
  59. channel.onmessage = (msg) => {
  60. UI.createMessageElement('from-them', msg.data);
  61. };
  62. };
  63. }
  64. const UI = {
  65. createMessageElement: function(style, message) {
  66. const conversation = document.getElementById('messages');
  67. const messageElement = document.createElement('p');
  68. messageElement.setAttribute('class', style);
  69. messageElement.append(message);
  70. conversation.append(messageElement);
  71. },
  72. messageInputValue: function() {
  73. return document.getElementById('sendData').value;
  74. },
  75. setConnectionState: function(state) {
  76. const stateEl = document.getElementById('connection-state');
  77. stateEl.innerHTML = state;
  78. }
  79. };
  80. function sendMessage(channel) {
  81. const message = UI.messageInputValue();
  82. UI.createMessageElement('from-me', message);
  83. channel.send(message);
  84. }
  85. function getQueryParameter(name) {
  86. const urlParams = new URLSearchParams(window.location.search);
  87. return urlParams.get(name);
  88. }