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

data_channels.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. updateLargeVideo(video[0].src);
  43. }
  44. }
  45. }
  46. };
  47. dataChannel.onclose = function ()
  48. {
  49. console.info("The Data Channel closed", dataChannel);
  50. };
  51. }
  52. /**
  53. * Binds "ondatachannel" event listener to given PeerConnection instance.
  54. * @param peerConnection WebRTC peer connection instance.
  55. */
  56. function bindDataChannelListener(peerConnection)
  57. {
  58. peerConnection.ondatachannel = onDataChannel;
  59. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  60. // Although it's not a requirement to open separate channels from both bridge
  61. // and peer as single channel can be used for sending and receiving data.
  62. // So either channel opened by the bridge or the one opened here is enough
  63. // for communication with the bridge.
  64. /*var dataChannelOptions =
  65. {
  66. reliable: true
  67. };
  68. var dataChannel
  69. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  70. // Can be used only when is in open state
  71. dataChannel.onopen = function ()
  72. {
  73. dataChannel.send("My channel !!!");
  74. };
  75. dataChannel.onmessage = function (event)
  76. {
  77. var msgData = event.data;
  78. console.info("Got My Data Channel Message:", msgData, dataChannel);
  79. };*/
  80. }