您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

data_channels.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Code sample for sending string and/or binary data
  14. // Sends String message to the bridge
  15. //dataChannel.send("Hello bridge!");
  16. // Sends 12 bytes binary message to the bridge
  17. //dataChannel.send(new ArrayBuffer(12));
  18. };
  19. dataChannel.onerror = function (error)
  20. {
  21. console.error("Data Channel Error:", error, dataChannel);
  22. };
  23. dataChannel.onmessage = function (event)
  24. {
  25. var msgData = event.data;
  26. console.info("Got Data Channel Message:", msgData, dataChannel);
  27. // Active speaker event
  28. if (msgData.indexOf('activeSpeaker') === 0 && !focusedVideoSrc)
  29. {
  30. // Endpoint ID from the bridge
  31. var endpointId = msgData.split(":")[1];
  32. console.info("New active speaker: " + endpointId);
  33. var container = document.getElementById(
  34. 'participant_' + endpointId);
  35. // Local video will not have container found, but that's ok
  36. // since we don't want to switch to local video
  37. if (container)
  38. {
  39. var video = container.getElementsByTagName("video");
  40. if (video.length)
  41. {
  42. VideoLayout.updateLargeVideo(video[0].src);
  43. VideoLayout.enableActiveSpeaker(endpointId, true);
  44. }
  45. }
  46. }
  47. };
  48. dataChannel.onclose = function ()
  49. {
  50. console.info("The Data Channel closed", dataChannel);
  51. };
  52. }
  53. /**
  54. * Binds "ondatachannel" event listener to given PeerConnection instance.
  55. * @param peerConnection WebRTC peer connection instance.
  56. */
  57. function bindDataChannelListener(peerConnection)
  58. {
  59. peerConnection.ondatachannel = onDataChannel;
  60. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  61. // Although it's not a requirement to open separate channels from both bridge
  62. // and peer as single channel can be used for sending and receiving data.
  63. // So either channel opened by the bridge or the one opened here is enough
  64. // for communication with the bridge.
  65. /*var dataChannelOptions =
  66. {
  67. reliable: true
  68. };
  69. var dataChannel
  70. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  71. // Can be used only when is in open state
  72. dataChannel.onopen = function ()
  73. {
  74. dataChannel.send("My channel !!!");
  75. };
  76. dataChannel.onmessage = function (event)
  77. {
  78. var msgData = event.data;
  79. console.info("Got My Data Channel Message:", msgData, dataChannel);
  80. };*/
  81. }