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

API.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* global APP */
  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. /**
  8. * List of the available commands.
  9. * @type {{
  10. * displayName: inputDisplayNameHandler,
  11. * toggleAudio: toggleAudio,
  12. * toggleVideo: toggleVideo,
  13. * toggleFilmStrip: toggleFilmStrip,
  14. * toggleChat: toggleChat,
  15. * toggleContactList: toggleContactList
  16. * }}
  17. */
  18. var commands = {};
  19. function initCommands() {
  20. commands = {
  21. displayName: APP.UI.inputDisplayNameHandler,
  22. toggleAudio: APP.conference.toggleAudioMuted,
  23. toggleVideo: APP.conference.toggleVideoMuted,
  24. toggleFilmStrip: APP.UI.toggleFilmStrip,
  25. toggleChat: APP.UI.toggleChat,
  26. toggleContactList: APP.UI.toggleContactList
  27. };
  28. }
  29. /**
  30. * Maps the supported events and their status
  31. * (true it the event is enabled and false if it is disabled)
  32. * @type {{
  33. * incomingMessage: boolean,
  34. * outgoingMessage: boolean,
  35. * displayNameChange: boolean,
  36. * participantJoined: boolean,
  37. * participantLeft: boolean
  38. * }}
  39. */
  40. const events = {
  41. incomingMessage: false,
  42. outgoingMessage:false,
  43. displayNameChange: false,
  44. participantJoined: false,
  45. participantLeft: false
  46. };
  47. /**
  48. * Processes commands from external application.
  49. * @param message the object with the command
  50. */
  51. function processCommand(message) {
  52. if (message.action != "execute") {
  53. console.error("Unknown action of the message");
  54. return;
  55. }
  56. for (var key in message) {
  57. if(commands[key])
  58. commands[key].apply(null, message[key]);
  59. }
  60. }
  61. /**
  62. * Processes events objects from external applications
  63. * @param event the event
  64. */
  65. function processEvent(event) {
  66. if (!event.action) {
  67. console.error("Event with no action is received.");
  68. return;
  69. }
  70. var i = 0;
  71. switch(event.action) {
  72. case "add":
  73. for (; i < event.events.length; i++) {
  74. events[event.events[i]] = true;
  75. }
  76. break;
  77. case "remove":
  78. for (; i < event.events.length; i++) {
  79. events[event.events[i]] = false;
  80. }
  81. break;
  82. default:
  83. console.error("Unknown action for event.");
  84. }
  85. }
  86. /**
  87. * Sends message to the external application.
  88. * @param object
  89. */
  90. function sendMessage(object) {
  91. window.parent.postMessage(JSON.stringify(object), "*");
  92. }
  93. /**
  94. * Processes a message event from the external application
  95. * @param event the message event
  96. */
  97. function processMessage(event) {
  98. var message;
  99. try {
  100. message = JSON.parse(event.data);
  101. } catch (e) {}
  102. if(!message.type)
  103. return;
  104. switch (message.type) {
  105. case "command":
  106. processCommand(message);
  107. break;
  108. case "event":
  109. processEvent(message);
  110. break;
  111. default:
  112. console.error("Unknown type of the message");
  113. return;
  114. }
  115. }
  116. /**
  117. * Check whether the API should be enabled or not.
  118. * @returns {boolean}
  119. */
  120. function isEnabled () {
  121. let hash = location.hash;
  122. return hash && hash.indexOf("external") > -1 && window.postMessage;
  123. }
  124. /**
  125. * Checks whether the event is enabled ot not.
  126. * @param name the name of the event.
  127. * @returns {*}
  128. */
  129. function isEventEnabled (name) {
  130. return events[name];
  131. }
  132. /**
  133. * Sends event object to the external application that has been subscribed
  134. * for that event.
  135. * @param name the name event
  136. * @param object data associated with the event
  137. */
  138. function triggerEvent (name, object) {
  139. if (isEnabled() && isEventEnabled(name)) {
  140. sendMessage({
  141. type: "event",
  142. action: "result",
  143. event: name,
  144. result: object
  145. });
  146. }
  147. }
  148. export default {
  149. /**
  150. * Initializes the APIConnector. Setups message event listeners that will
  151. * receive information from external applications that embed Jitsi Meet.
  152. * It also sends a message to the external application that APIConnector
  153. * is initialized.
  154. */
  155. init: function () {
  156. if (!isEnabled()) {
  157. return;
  158. }
  159. initCommands();
  160. if (window.addEventListener) {
  161. window.addEventListener('message', processMessage, false);
  162. } else {
  163. window.attachEvent('onmessage', processMessage);
  164. }
  165. sendMessage({type: "system", loaded: true});
  166. },
  167. /**
  168. * Notify external application (if API is enabled) that message was sent.
  169. * @param {string} body message body
  170. */
  171. notifySendingChatMessage (body) {
  172. triggerEvent("outgoingMessage", {"message": body});
  173. },
  174. /**
  175. * Notify external application (if API is enabled) that
  176. * message was received.
  177. * @param {string} id user id
  178. * @param {string} nick user nickname
  179. * @param {string} body message body
  180. * @param {number} ts message creation timestamp
  181. */
  182. notifyReceivedChatMessage (id, nick, body, ts) {
  183. if (APP.conference.isLocalId(id)) {
  184. return;
  185. }
  186. triggerEvent(
  187. "incomingMessage",
  188. {"from": id, "nick": nick, "message": body, "stamp": ts}
  189. );
  190. },
  191. /**
  192. * Notify external application (if API is enabled) that
  193. * user joined the conference.
  194. * @param {string} id user id
  195. */
  196. notifyUserJoined (id) {
  197. triggerEvent("participantJoined", {id});
  198. },
  199. /**
  200. * Notify external application (if API is enabled) that
  201. * user left the conference.
  202. * @param {string} id user id
  203. */
  204. notifyUserLeft (id) {
  205. triggerEvent("participantLeft", {id});
  206. },
  207. /**
  208. * Notify external application (if API is enabled) that
  209. * user changed their nickname.
  210. * @param {string} id user id
  211. * @param {string} displayName user nickname
  212. */
  213. notifyDisplayNameChanged (id, displayName) {
  214. triggerEvent("displayNameChange", {id, displayname: displayName});
  215. },
  216. /**
  217. * Removes the listeners.
  218. */
  219. dispose: function () {
  220. if (!isEnabled()) {
  221. return;
  222. }
  223. if (window.removeEventListener) {
  224. window.removeEventListener("message", processMessage, false);
  225. } else {
  226. window.detachEvent('onmessage', processMessage);
  227. }
  228. }
  229. };