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

data_channels.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Callback triggered by PeerConnection when new data channel is opened
  3. * on the bridge.
  4. * @param event the event info object.
  5. */
  6. function onDataChannel(event)
  7. {
  8. var dataChannel = event.channel;
  9. dataChannel.onopen = function ()
  10. {
  11. console.info("Data channel opened by the bridge !!!", dataChannel);
  12. // Sends String message to the bridge
  13. dataChannel.send("Hello bridge!");
  14. // Sends 12 bytes binary message to the bridge
  15. dataChannel.send(new ArrayBuffer(12));
  16. };
  17. dataChannel.onerror = function (error)
  18. {
  19. console.error("Data Channel Error:", error, dataChannel);
  20. };
  21. dataChannel.onmessage = function (event)
  22. {
  23. var msgData = event.data;
  24. console.info("Got Data Channel Message:", msgData, dataChannel);
  25. };
  26. dataChannel.onclose = function ()
  27. {
  28. console.info("The Data Channel closed", dataChannel);
  29. };
  30. }
  31. /**
  32. * Binds "ondatachannel" event listener to given PeerConnection instance.
  33. * @param peerConnection WebRTC peer connection instance.
  34. */
  35. function bindDataChannelListener(peerConnection)
  36. {
  37. peerConnection.ondatachannel = onDataChannel;
  38. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  39. // Although it's not a requirement to open separate channels from both bridge
  40. // and peer as single channel can be used for sending and receiving data.
  41. // So either channel opened by the bridge or the one opened here is enough
  42. // for communication with the bridge.
  43. var dataChannelOptions =
  44. {
  45. reliable: true
  46. };
  47. var dataChannel
  48. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  49. // Can be used only when is in open state
  50. dataChannel.onopen = function ()
  51. {
  52. dataChannel.send("My channel !!!");
  53. };
  54. }