Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PostMessageTransportBackend.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Postis from 'postis';
  2. /**
  3. * The list of methods of incomming postis messages that we have to support for
  4. * backward compatability for the users that are directly sending messages to
  5. * Jitsi Meet (without using external_api.js)
  6. *
  7. * @type {string[]}
  8. */
  9. const legacyIncomingMethods = [ 'display-name', 'toggle-audio', 'toggle-video',
  10. 'toggle-film-strip', 'toggle-chat', 'toggle-contact-list',
  11. 'toggle-share-screen', 'video-hangup', 'email', 'avatar-url' ];
  12. /**
  13. * The list of methods of outgoing postis messages that we have to support for
  14. * backward compatability for the users that are directly listening to the
  15. * postis messages send by Jitsi Meet(without using external_api.js).
  16. *
  17. * @type {string[]}
  18. */
  19. const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message',
  20. 'outgoing-message', 'participant-joined', 'participant-left',
  21. 'video-ready-to-close', 'video-conference-joined',
  22. 'video-conference-left' ];
  23. /**
  24. * The postis method used for all messages.
  25. *
  26. * @type {string}
  27. */
  28. const POSTIS_METHOD_NAME = 'data';
  29. /**
  30. * The default options for postis.
  31. *
  32. * @type {Object}
  33. */
  34. const defaultPostisOptions = {
  35. window: window.opener || window.parent
  36. };
  37. /**
  38. * Implements message transport using the postMessage API.
  39. */
  40. export default class PostMessageTransportBackend {
  41. /**
  42. * Creates new PostMessageTransportBackend instance.
  43. *
  44. * @param {Object} options - Optional parameters for configuration of the
  45. * transport.
  46. */
  47. constructor(options = {}) {
  48. const postisOptions = Object.assign({}, defaultPostisOptions,
  49. options.postisOptions);
  50. this.postis = Postis(postisOptions);
  51. this._enableLegacyFormat = options.enableLegacyFormat;
  52. if (!this._enableLegacyFormat) {
  53. // backward compatability
  54. legacyIncomingMethods.forEach(method =>
  55. this.postis.listen(method,
  56. params => this._onPostisDataReceived(method, params)));
  57. }
  58. this.postis.listen(POSTIS_METHOD_NAME, data =>
  59. this._dataReceivedCallBack(data));
  60. this._dataReceivedCallBack = () => {
  61. // do nothing until real callback is set;
  62. };
  63. }
  64. /**
  65. * Handles incomming legacy postis data.
  66. *
  67. * @param {string} method - The method property from postis data object.
  68. * @param {Any} params - The params property from postis data object.
  69. * @returns {void}
  70. */
  71. _onPostisDataReceived(method, params = {}) {
  72. const newData = {
  73. data: {
  74. name: method,
  75. data: params
  76. }
  77. };
  78. this._dataReceivedCallBack(newData);
  79. }
  80. /**
  81. * Sends the passed data via postis using the old format.
  82. *
  83. * @param {Object} data - The data to be sent.
  84. * @returns {void}
  85. */
  86. _sendLegacyData(data) {
  87. const method = data.name;
  88. if (method && legacyOutgoingMethods.indexOf(method) !== -1) {
  89. this.postis.send({
  90. method,
  91. params: data.data
  92. });
  93. }
  94. }
  95. /**
  96. * Disposes the allocated resources.
  97. *
  98. * @returns {void}
  99. */
  100. dispose() {
  101. this.postis.destroy();
  102. }
  103. /**
  104. * Sends the passed data.
  105. *
  106. * @param {Object} data - The data to be sent.
  107. * @returns {void}
  108. */
  109. send(data) {
  110. this.postis.send({
  111. method: POSTIS_METHOD_NAME,
  112. params: data
  113. });
  114. if (!this._enableLegacyFormat) {
  115. // For the legacy use case we don't need any new fields defined in
  116. // Transport class. That's why we are passing only the original
  117. // object passed by the consumer of the Transport class which is
  118. // data.data.
  119. this._sendLegacyData(data.data);
  120. }
  121. }
  122. /**
  123. * Sets the callback for receiving data.
  124. *
  125. * @param {Function} callback - The new callback.
  126. * @returns {void}
  127. */
  128. setDataReceivedCallback(callback) {
  129. this._dataReceivedCallBack = callback;
  130. }
  131. }