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

API.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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,
  41. "toggle-video": APP.conference.toggleVideoMuted,
  42. "toggle-film-strip": APP.UI.toggleFilmStrip,
  43. "toggle-chat": APP.UI.toggleChat,
  44. "toggle-contact-list": APP.UI.toggleContactList,
  45. "toggle-share-screen": APP.conference.toggleScreenSharing,
  46. "video-hangup": () => APP.conference.hangup()
  47. };
  48. Object.keys(commands).forEach(function (key) {
  49. postis.listen(key, commands[key]);
  50. });
  51. }
  52. /**
  53. * Maps the supported events and their status
  54. * (true it the event is enabled and false if it is disabled)
  55. * @type {{
  56. * incoming-message: boolean,
  57. * outgoing-message: boolean,
  58. * display-name-change: boolean,
  59. * participant-left: boolean,
  60. * participant-joined: boolean,
  61. * video-conference-left: boolean,
  62. * video-conference-joined: boolean
  63. * }}
  64. */
  65. const events = {
  66. "incoming-message": false,
  67. "outgoing-message":false,
  68. "display-name-change": false,
  69. "participant-joined": false,
  70. "participant-left": false,
  71. "video-conference-joined": false,
  72. "video-conference-left": false,
  73. "video-ready-to-close": false
  74. };
  75. /**
  76. * Sends message to the external application.
  77. * @param message {object}
  78. * @param method {string}
  79. * @param params {object} the object that will be sent as JSON string
  80. */
  81. function sendMessage(message) {
  82. if(enabled)
  83. postis.send(message);
  84. }
  85. /**
  86. * Check whether the API should be enabled or not.
  87. * @returns {boolean}
  88. */
  89. function isEnabled () {
  90. return (typeof jitsi_meet_external_api_id === "number");
  91. }
  92. /**
  93. * Checks whether the event is enabled ot not.
  94. * @param name the name of the event.
  95. * @returns {*}
  96. */
  97. function isEventEnabled (name) {
  98. return events[name];
  99. }
  100. /**
  101. * Sends event object to the external application that has been subscribed
  102. * for that event.
  103. * @param name the name event
  104. * @param object data associated with the event
  105. */
  106. function triggerEvent (name, object) {
  107. if(isEventEnabled(name))
  108. sendMessage({method: name, params: object});
  109. }
  110. /**
  111. * Handles system messages. (for example: enable/disable events)
  112. * @param message {object} the message
  113. */
  114. function onSystemMessage(message) {
  115. switch (message.type) {
  116. case "eventStatus":
  117. if(!message.name || !message.value) {
  118. console.warn("Unknown system message format", message);
  119. break;
  120. }
  121. events[message.name] = message.value;
  122. break;
  123. default:
  124. console.warn("Unknown system message type", message);
  125. }
  126. }
  127. export default {
  128. /**
  129. * Initializes the APIConnector. Setups message event listeners that will
  130. * receive information from external applications that embed Jitsi Meet.
  131. * It also sends a message to the external application that APIConnector
  132. * is initialized.
  133. * @param options {object}
  134. * @param forceEnable {boolean} if true the module will be enabled.
  135. * @param enabledEvents {array} array of events that should be enabled.
  136. */
  137. init (options = {}) {
  138. if(!isEnabled() && !options.forceEnable)
  139. return;
  140. enabled = true;
  141. if(options.enabledEvents)
  142. options.enabledEvents.forEach(function (eventName) {
  143. events[eventName] = true;
  144. });
  145. let postisOptions = {
  146. window: target
  147. };
  148. if(typeof jitsi_meet_external_api_id === "number")
  149. postisOptions.scope
  150. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  151. postis = postisInit(postisOptions);
  152. postis.listen("jitsiSystemMessage", onSystemMessage);
  153. initCommands();
  154. },
  155. /**
  156. * Notify external application (if API is enabled) that message was sent.
  157. * @param {string} body message body
  158. */
  159. notifySendingChatMessage (body) {
  160. triggerEvent("outgoing-message", {"message": body});
  161. },
  162. /**
  163. * Notify external application (if API is enabled) that
  164. * message was received.
  165. * @param {string} id user id
  166. * @param {string} nick user nickname
  167. * @param {string} body message body
  168. * @param {number} ts message creation timestamp
  169. */
  170. notifyReceivedChatMessage (id, nick, body, ts) {
  171. if (APP.conference.isLocalId(id)) {
  172. return;
  173. }
  174. triggerEvent(
  175. "incoming-message",
  176. {"from": id, "nick": nick, "message": body, "stamp": ts}
  177. );
  178. },
  179. /**
  180. * Notify external application (if API is enabled) that
  181. * user joined the conference.
  182. * @param {string} id user id
  183. */
  184. notifyUserJoined (id) {
  185. triggerEvent("participant-joined", {id});
  186. },
  187. /**
  188. * Notify external application (if API is enabled) that
  189. * user left the conference.
  190. * @param {string} id user id
  191. */
  192. notifyUserLeft (id) {
  193. triggerEvent("participant-left", {id});
  194. },
  195. /**
  196. * Notify external application (if API is enabled) that
  197. * user changed their nickname.
  198. * @param {string} id user id
  199. * @param {string} displayName user nickname
  200. */
  201. notifyDisplayNameChanged (id, displayName) {
  202. triggerEvent("display-name-change", {id, displayname: displayName});
  203. },
  204. /**
  205. * Notify external application (if API is enabled) that
  206. * user changed their nickname.
  207. * @param {string} id user id
  208. * @param {string} displayName user nickname
  209. */
  210. notifyConferenceJoined (room) {
  211. triggerEvent("video-conference-joined", {roomName: room});
  212. },
  213. /**
  214. * Notify external application (if API is enabled) that
  215. * user changed their nickname.
  216. * @param {string} id user id
  217. * @param {string} displayName user nickname
  218. */
  219. notifyConferenceLeft (room) {
  220. triggerEvent("video-conference-left", {roomName: room});
  221. },
  222. /**
  223. * Notify external application (if API is enabled) that
  224. * we are ready to be closed.
  225. */
  226. notifyReadyToClose () {
  227. triggerEvent("video-ready-to-close", {});
  228. },
  229. /**
  230. * Removes the listeners.
  231. */
  232. dispose: function () {
  233. if(enabled)
  234. postis.destroy();
  235. }
  236. };