選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

data_channels.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. console.info("Got Data Channel Message:", data, dataChannel);
  27. // Active speaker event
  28. if (data.indexOf('activeSpeaker') === 0)
  29. {
  30. // Endpoint ID from the Videobridge.
  31. var resourceJid = data.split(":")[1];
  32. console.info(
  33. "Data channel new active speaker event: " + resourceJid);
  34. $(document).trigger('activespeakerchanged', [resourceJid]);
  35. }
  36. else
  37. {
  38. // JSON
  39. var obj;
  40. try
  41. {
  42. obj = JSON.parse(data);
  43. }
  44. catch (e)
  45. {
  46. console.error(
  47. "Failed to parse data channel message as JSON: ",
  48. data,
  49. dataChannel);
  50. }
  51. if (('undefined' !== typeof(obj)) && (null !== obj))
  52. {
  53. // TODO Consume the JSON-formatted data channel message.
  54. console.debug("Data channel JSON-formatted message: ", obj);
  55. }
  56. }
  57. };
  58. dataChannel.onclose = function ()
  59. {
  60. console.info("The Data Channel closed", dataChannel);
  61. };
  62. }
  63. /**
  64. * Binds "ondatachannel" event listener to given PeerConnection instance.
  65. * @param peerConnection WebRTC peer connection instance.
  66. */
  67. function bindDataChannelListener(peerConnection)
  68. {
  69. peerConnection.ondatachannel = onDataChannel;
  70. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  71. // Although it's not a requirement to open separate channels from both bridge
  72. // and peer as single channel can be used for sending and receiving data.
  73. // So either channel opened by the bridge or the one opened here is enough
  74. // for communication with the bridge.
  75. /*var dataChannelOptions =
  76. {
  77. reliable: true
  78. };
  79. var dataChannel
  80. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  81. // Can be used only when is in open state
  82. dataChannel.onopen = function ()
  83. {
  84. dataChannel.send("My channel !!!");
  85. };
  86. dataChannel.onmessage = function (event)
  87. {
  88. var msgData = event.data;
  89. console.info("Got My Data Channel Message:", msgData, dataChannel);
  90. };*/
  91. }