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

data_channels.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Videobridge!", 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 data = event.data;
  26. // JSON
  27. var obj;
  28. try
  29. {
  30. obj = JSON.parse(data);
  31. }
  32. catch (e)
  33. {
  34. console.error(
  35. "Failed to parse data channel message as JSON: ",
  36. data,
  37. dataChannel);
  38. }
  39. if (('undefined' !== typeof(obj)) && (null !== obj))
  40. {
  41. var colibriClass = obj.colibriClass;
  42. if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
  43. {
  44. // Endpoint ID from the Videobridge.
  45. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  46. console.info(
  47. "Data channel new dominant speaker event: ",
  48. dominantSpeakerEndpoint);
  49. $(document).trigger(
  50. 'dominantspeakerchanged',
  51. [dominantSpeakerEndpoint]);
  52. }
  53. else if ("LastNEndpointsChangeEvent" === colibriClass)
  54. {
  55. // The new/latest list of last-n endpoint IDs.
  56. var lastNEndpoints = obj.lastNEndpoints;
  57. /*
  58. * The list of endpoint IDs which are entering the list of
  59. * last-n at this time i.e. were not in the old list of last-n
  60. * endpoint IDs.
  61. */
  62. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  63. console.debug(
  64. "Data channel new last-n event: ",
  65. lastNEndpoints);
  66. }
  67. else
  68. {
  69. console.debug("Data channel JSON-formatted message: ", obj);
  70. }
  71. }
  72. };
  73. dataChannel.onclose = function ()
  74. {
  75. console.info("The Data Channel closed", dataChannel);
  76. };
  77. }
  78. /**
  79. * Binds "ondatachannel" event listener to given PeerConnection instance.
  80. * @param peerConnection WebRTC peer connection instance.
  81. */
  82. function bindDataChannelListener(peerConnection)
  83. {
  84. peerConnection.ondatachannel = onDataChannel;
  85. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  86. // Although it's not a requirement to open separate channels from both bridge
  87. // and peer as single channel can be used for sending and receiving data.
  88. // So either channel opened by the bridge or the one opened here is enough
  89. // for communication with the bridge.
  90. /*var dataChannelOptions =
  91. {
  92. reliable: true
  93. };
  94. var dataChannel
  95. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  96. // Can be used only when is in open state
  97. dataChannel.onopen = function ()
  98. {
  99. dataChannel.send("My channel !!!");
  100. };
  101. dataChannel.onmessage = function (event)
  102. {
  103. var msgData = event.data;
  104. console.info("Got My Data Channel Message:", msgData, dataChannel);
  105. };*/
  106. }