answerScript.js 3.2 KB

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