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

API.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* global APP, getConfigParamsFromUrl */
  2. /**
  3. * Implements API class that communicates with external api class
  4. * and provides interface to access Jitsi Meet features by external
  5. * applications that embed Jitsi Meet
  6. */
  7. import postisInit from 'postis';
  8. /**
  9. * List of the available commands.
  10. * @type {{
  11. * displayName: inputDisplayNameHandler,
  12. * toggleAudio: toggleAudio,
  13. * toggleVideo: toggleVideo,
  14. * toggleFilmStrip: toggleFilmStrip,
  15. * toggleChat: toggleChat,
  16. * toggleContactList: toggleContactList
  17. * }}
  18. */
  19. let commands = {};
  20. let hashParams = getConfigParamsFromUrl();
  21. /**
  22. * JitsiMeetExternalAPI id - unique for a webpage.
  23. */
  24. let jitsi_meet_external_api_id = hashParams.jitsi_meet_external_api_id;
  25. /**
  26. * Object that will execute sendMessage
  27. */
  28. let target = window.opener ? window.opener : window.parent;
  29. /**
  30. * Postis instance. Used to communicate with the external application.
  31. */
  32. let postis;
  33. /**
  34. * Current status (enabled/disabled) of API.
  35. */
  36. let enabled = false;
  37. function initCommands() {
  38. commands = {
  39. "display-name": APP.UI.inputDisplayNameHandler,
  40. "toggle-audio": APP.conference.toggleAudioMuted.bind(APP.conference),
  41. "toggle-video": APP.conference.toggleVideoMuted.bind(APP.conference),
  42. "toggle-film-strip": APP.UI.toggleFilmStrip,
  43. "toggle-chat": APP.UI.toggleChat,
  44. "toggle-contact-list": APP.UI.toggleContactList,
  45. "toggle-share-screen":
  46. APP.conference.toggleScreenSharing.bind(APP.conference),
  47. "video-hangup": () => APP.conference.hangup(),
  48. "email": APP.conference.changeLocalEmail,
  49. "avatar-url": APP.conference.changeLocalAvatarUrl
  50. };
  51. Object.keys(commands).forEach(function (key) {
  52. postis.listen(key, args => commands[key](...args));
  53. });
  54. }
  55. /**
  56. * Sends message to the external application.
  57. * @param message {object}
  58. * @param method {string}
  59. * @param params {object} the object that will be sent as JSON string
  60. */
  61. function sendMessage(message) {
  62. if(enabled) {
  63. postis.send(message);
  64. }
  65. }
  66. /**
  67. * Check whether the API should be enabled or not.
  68. * @returns {boolean}
  69. */
  70. function shouldBeEnabled () {
  71. return (typeof jitsi_meet_external_api_id === "number");
  72. }
  73. /**
  74. * Sends event object to the external application that has been subscribed
  75. * for that event.
  76. * @param name the name event
  77. * @param object data associated with the event
  78. */
  79. function triggerEvent (name, object) {
  80. if(enabled) {
  81. sendMessage({method: name, params: object});
  82. }
  83. }
  84. export default {
  85. /**
  86. * Initializes the APIConnector. Setups message event listeners that will
  87. * receive information from external applications that embed Jitsi Meet.
  88. * It also sends a message to the external application that APIConnector
  89. * is initialized.
  90. * @param options {object}
  91. * @param forceEnable {boolean} if true the module will be enabled.
  92. */
  93. init (options = {}) {
  94. if(!shouldBeEnabled() && !options.forceEnable)
  95. return;
  96. enabled = true;
  97. let postisOptions = {
  98. window: target
  99. };
  100. if(typeof jitsi_meet_external_api_id === "number")
  101. postisOptions.scope
  102. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  103. postis = postisInit(postisOptions);
  104. initCommands();
  105. },
  106. /**
  107. * Notify external application (if API is enabled) that message was sent.
  108. * @param {string} body message body
  109. */
  110. notifySendingChatMessage (body) {
  111. triggerEvent("outgoing-message", {"message": body});
  112. },
  113. /**
  114. * Notify external application (if API is enabled) that
  115. * message was received.
  116. * @param {string} id user id
  117. * @param {string} nick user nickname
  118. * @param {string} body message body
  119. * @param {number} ts message creation timestamp
  120. */
  121. notifyReceivedChatMessage (id, nick, body, ts) {
  122. if (APP.conference.isLocalId(id)) {
  123. return;
  124. }
  125. triggerEvent(
  126. "incoming-message",
  127. {"from": id, "nick": nick, "message": body, "stamp": ts}
  128. );
  129. },
  130. /**
  131. * Notify external application (if API is enabled) that
  132. * user joined the conference.
  133. * @param {string} id user id
  134. */
  135. notifyUserJoined (id) {
  136. triggerEvent("participant-joined", {id});
  137. },
  138. /**
  139. * Notify external application (if API is enabled) that
  140. * user left the conference.
  141. * @param {string} id user id
  142. */
  143. notifyUserLeft (id) {
  144. triggerEvent("participant-left", {id});
  145. },
  146. /**
  147. * Notify external application (if API is enabled) that
  148. * user changed their nickname.
  149. * @param {string} id user id
  150. * @param {string} displayName user nickname
  151. */
  152. notifyDisplayNameChanged (id, displayName) {
  153. triggerEvent("display-name-change", {id, displayname: displayName});
  154. },
  155. /**
  156. * Notify external application (if API is enabled) that
  157. * user changed their nickname.
  158. * @param {string} id user id
  159. * @param {string} displayName user nickname
  160. */
  161. notifyConferenceJoined (room) {
  162. triggerEvent("video-conference-joined", {roomName: room});
  163. },
  164. /**
  165. * Notify external application (if API is enabled) that
  166. * user changed their nickname.
  167. * @param {string} id user id
  168. * @param {string} displayName user nickname
  169. */
  170. notifyConferenceLeft (room) {
  171. triggerEvent("video-conference-left", {roomName: room});
  172. },
  173. /**
  174. * Notify external application (if API is enabled) that
  175. * we are ready to be closed.
  176. */
  177. notifyReadyToClose () {
  178. triggerEvent("video-ready-to-close", {});
  179. },
  180. /**
  181. * Removes the listeners.
  182. */
  183. dispose: function () {
  184. if(enabled)
  185. postis.destroy();
  186. }
  187. };