script.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * libdatachannel example web client
  3. * Copyright (C) 2020 Lara Mackey
  4. * Copyright (C) 2020 Paul-Louis Ageneau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. window.addEventListener('load', () => {
  20. const config = {
  21. iceServers: [{
  22. urls: 'stun:stun.l.google.com:19302', // change to your STUN server
  23. }],
  24. };
  25. const localId = randomId(4);
  26. const url = `ws://localhost:8000/${localId}`;
  27. const peerConnectionMap = {};
  28. const dataChannelMap = {};
  29. const offerId = document.getElementById('offerId');
  30. const offerBtn = document.getElementById('offerBtn');
  31. const sendMsg = document.getElementById('sendMsg');
  32. const sendBtn = document.getElementById('sendBtn');
  33. const _localId = document.getElementById('localId');
  34. _localId.textContent = localId;
  35. console.log('Connecting to signaling...');
  36. openSignaling(url)
  37. .then((ws) => {
  38. console.log('WebSocket connected, signaling ready');
  39. offerId.disabled = false;
  40. offerBtn.disabled = false;
  41. offerBtn.onclick = () => offerPeerConnection(ws, offerId.value);
  42. })
  43. .catch((err) => console.error(err));
  44. function openSignaling(url) {
  45. return new Promise((resolve, reject) => {
  46. const ws = new WebSocket(url);
  47. ws.onopen = () => resolve(ws);
  48. ws.onerror = () => reject(new Error('WebSocket error'));
  49. ws.onclose = () => console.error('WebSocket disconnected');
  50. ws.onmessage = (e) => {
  51. if(typeof(e.data) != 'string') return;
  52. const message = JSON.parse(e.data);
  53. console.log(message);
  54. const { id, type } = message;
  55. let pc = peerConnectionMap[id];
  56. if(!pc) {
  57. if(type != 'offer') return;
  58. // Create PeerConnection for answer
  59. console.log(`Answering to ${id}`);
  60. pc = createPeerConnection(ws, id);
  61. }
  62. switch(type) {
  63. case 'offer':
  64. case 'answer':
  65. pc.setRemoteDescription({
  66. sdp: message.description,
  67. type: message.type,
  68. })
  69. .then(() => {
  70. if(type == 'offer') {
  71. // Send answer
  72. sendLocalDescription(ws, id, pc, 'answer');
  73. }
  74. });
  75. break;
  76. case 'candidate':
  77. pc.addIceCandidate({
  78. candidate: message.candidate,
  79. sdpMid: message.mid,
  80. });
  81. break;
  82. }
  83. }
  84. });
  85. }
  86. function offerPeerConnection(ws, id) {
  87. // Create PeerConnection
  88. console.log(`Offering to ${id}`);
  89. pc = createPeerConnection(ws, id);
  90. // Create DataChannel
  91. const label = "test";
  92. console.log(`Creating DataChannel with label "${label}"`);
  93. const dc = pc.createDataChannel(label);
  94. setupDataChannel(dc, id);
  95. // Send offer
  96. sendLocalDescription(ws, id, pc, 'offer');
  97. }
  98. // Create and setup a PeerConnection
  99. function createPeerConnection(ws, id) {
  100. const pc = new RTCPeerConnection(config);
  101. pc.onconnectionstatechange = () => console.log(`Connection state: ${pc.connectionState}`);
  102. pc.onicegatheringstatechange = () => console.log(`Gathering state: ${pc.iceGatheringState}`);
  103. pc.onicecandidate = (e) => {
  104. if (e.candidate && e.candidate.candidate) {
  105. // Send candidate
  106. sendLocalCandidate(ws, id, e.candidate);
  107. }
  108. };
  109. pc.ondatachannel = (e) => {
  110. const dc = e.channel;
  111. console.log(`"DataChannel from ${id} received with label "${dc.label}"`);
  112. setupDataChannel(dc, id);
  113. dc.send(`Hello from ${localId}`);
  114. sendMsg.disabled = false;
  115. sendBtn.disabled = false;
  116. sendBtn.onclick = () => dc.send(sendMsg.value);
  117. };
  118. peerConnectionMap[id] = pc;
  119. return pc;
  120. }
  121. // Setup a DataChannel
  122. function setupDataChannel(dc, id) {
  123. dc.onopen = () => {
  124. console.log(`DataChannel from ${id} open`);
  125. sendMsg.disabled = false;
  126. sendBtn.disabled = false;
  127. sendBtn.onclick = () => dc.send(sendMsg.value);
  128. };
  129. dc.onclose = () => {
  130. console.log(`DataChannel from ${id} closed`);
  131. };
  132. dc.onmessage = (e) => {
  133. if(typeof(e.data) != 'string') return;
  134. console.log(`Message from ${id} received: ${e.data}`);
  135. document.body.appendChild(document.createTextNode(e.data));
  136. };
  137. dataChannelMap[id] = dc;
  138. return dc;
  139. }
  140. function sendLocalDescription(ws, id, pc, type) {
  141. (type == 'offer' ? pc.createOffer() : pc.createAnswer())
  142. .then((desc) => pc.setLocalDescription(desc))
  143. .then(() => {
  144. const { sdp, type } = pc.localDescription;
  145. ws.send(JSON.stringify({
  146. id,
  147. type,
  148. description: sdp,
  149. }));
  150. });
  151. }
  152. function sendLocalCandidate(ws, id, cand) {
  153. const {candidate, sdpMid} = cand;
  154. ws.send(JSON.stringify({
  155. id,
  156. type: 'candidate',
  157. candidate,
  158. mid: sdpMid,
  159. }));
  160. }
  161. // Helper function to generate a random ID
  162. function randomId(length) {
  163. const characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  164. const pickRandom = () => characters.charAt(Math.floor(Math.random() * characters.length));
  165. return [...Array(length)].map(pickRandom).join('');
  166. }
  167. });