You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_channels.js 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* global connection, Strophe, updateLargeVideo, focusedVideoSrc*/
  2. /**
  3. * Callback triggered by PeerConnection when new data channel is opened
  4. * on the bridge.
  5. * @param event the event info object.
  6. */
  7. function onDataChannel(event)
  8. {
  9. var dataChannel = event.channel;
  10. dataChannel.onopen = function ()
  11. {
  12. console.info("Data channel opened by the bridge !!!", dataChannel);
  13. // Sends String message to the bridge
  14. dataChannel.send("Hello bridge!");
  15. // Sends 12 bytes binary message to the bridge
  16. dataChannel.send(new ArrayBuffer(12));
  17. };
  18. dataChannel.onerror = function (error)
  19. {
  20. console.error("Data Channel Error:", error, dataChannel);
  21. };
  22. dataChannel.onmessage = function (event)
  23. {
  24. var msgData = event.data;
  25. console.info("Got Data Channel Message:", msgData, dataChannel);
  26. // Active speaker event
  27. if (msgData.indexOf('activeSpeaker') === 0 && !focusedVideoSrc)
  28. {
  29. // Endpoint ID from the bridge
  30. var endpointId = msgData.split(":")[1];
  31. console.info("New active speaker: " + endpointId);
  32. var container = document.getElementById(
  33. 'participant_' + endpointId);
  34. // Check if local video
  35. if (!container)
  36. {
  37. if (endpointId ===
  38. Strophe.getResourceFromJid(connection.emuc.myroomjid))
  39. {
  40. container = document.getElementById('localVideoContainer');
  41. }
  42. }
  43. if (container)
  44. {
  45. var video = container.getElementsByTagName("video");
  46. if (video.length)
  47. {
  48. updateLargeVideo(video[0].src);
  49. }
  50. }
  51. }
  52. };
  53. dataChannel.onclose = function ()
  54. {
  55. console.info("The Data Channel closed", dataChannel);
  56. };
  57. }
  58. /**
  59. * Binds "ondatachannel" event listener to given PeerConnection instance.
  60. * @param peerConnection WebRTC peer connection instance.
  61. */
  62. function bindDataChannelListener(peerConnection)
  63. {
  64. peerConnection.ondatachannel = onDataChannel;
  65. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  66. // Although it's not a requirement to open separate channels from both bridge
  67. // and peer as single channel can be used for sending and receiving data.
  68. // So either channel opened by the bridge or the one opened here is enough
  69. // for communication with the bridge.
  70. /*var dataChannelOptions =
  71. {
  72. reliable: true
  73. };
  74. var dataChannel
  75. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  76. // Can be used only when is in open state
  77. dataChannel.onopen = function ()
  78. {
  79. dataChannel.send("My channel !!!");
  80. };
  81. dataChannel.onmessage = function (event)
  82. {
  83. var msgData = event.data;
  84. console.info("Got My Data Channel Message:", msgData, dataChannel);
  85. };*/
  86. }