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

API.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  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. var commands = {};
  20. function initCommands() {
  21. commands = {
  22. displayName: APP.UI.inputDisplayNameHandler,
  23. toggleAudio: APP.UI.toggleAudio,
  24. toggleVideo: APP.UI.toggleVideo,
  25. toggleFilmStrip: APP.UI.toggleFilmStrip,
  26. toggleChat: APP.UI.toggleChat,
  27. toggleContactList: APP.UI.toggleContactList
  28. };
  29. }
  30. /**
  31. * Maps the supported events and their status
  32. * (true it the event is enabled and false if it is disabled)
  33. * @type {{
  34. * incomingMessage: boolean,
  35. * outgoingMessage: boolean,
  36. * displayNameChange: boolean,
  37. * participantJoined: boolean,
  38. * participantLeft: boolean
  39. * }}
  40. */
  41. var events = {
  42. incomingMessage: false,
  43. outgoingMessage:false,
  44. displayNameChange: false,
  45. participantJoined: false,
  46. participantLeft: false
  47. };
  48. var displayName = {};
  49. /**
  50. * Processes commands from external application.
  51. * @param message the object with the command
  52. */
  53. function processCommand(message) {
  54. if (message.action != "execute") {
  55. console.error("Unknown action of the message");
  56. return;
  57. }
  58. for (var key in message) {
  59. if(commands[key])
  60. commands[key].apply(null, message[key]);
  61. }
  62. }
  63. /**
  64. * Processes events objects from external applications
  65. * @param event the event
  66. */
  67. function processEvent(event) {
  68. if (!event.action) {
  69. console.error("Event with no action is received.");
  70. return;
  71. }
  72. var i = 0;
  73. switch(event.action) {
  74. case "add":
  75. for (; i < event.events.length; i++) {
  76. events[event.events[i]] = true;
  77. }
  78. break;
  79. case "remove":
  80. for (; i < event.events.length; i++) {
  81. events[event.events[i]] = false;
  82. }
  83. break;
  84. default:
  85. console.error("Unknown action for event.");
  86. }
  87. }
  88. /**
  89. * Sends message to the external application.
  90. * @param object
  91. */
  92. function sendMessage(object) {
  93. window.parent.postMessage(JSON.stringify(object), "*");
  94. }
  95. /**
  96. * Processes a message event from the external application
  97. * @param event the message event
  98. */
  99. function processMessage(event) {
  100. var message;
  101. try {
  102. message = JSON.parse(event.data);
  103. } catch (e) {}
  104. if(!message.type)
  105. return;
  106. switch (message.type) {
  107. case "command":
  108. processCommand(message);
  109. break;
  110. case "event":
  111. processEvent(message);
  112. break;
  113. default:
  114. console.error("Unknown type of the message");
  115. return;
  116. }
  117. }
  118. function setupListeners() {
  119. APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_JOINED, function (from) {
  120. API.triggerEvent("participantJoined", {jid: from});
  121. });
  122. APP.xmpp.addListener(XMPPEvents.MESSAGE_RECEIVED, function (from, nick, txt, myjid, stamp) {
  123. if (from != myjid)
  124. API.triggerEvent("incomingMessage",
  125. {"from": from, "nick": nick, "message": txt, "stamp": stamp});
  126. });
  127. APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_LEFT, function (jid) {
  128. API.triggerEvent("participantLeft", {jid: jid});
  129. });
  130. APP.xmpp.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, function (jid, newDisplayName) {
  131. var name = displayName[jid];
  132. if(!name || name != newDisplayName) {
  133. API.triggerEvent("displayNameChange", {jid: jid, displayname: newDisplayName});
  134. displayName[jid] = newDisplayName;
  135. }
  136. });
  137. APP.xmpp.addListener(XMPPEvents.SENDING_CHAT_MESSAGE, function (body) {
  138. APP.API.triggerEvent("outgoingMessage", {"message": body});
  139. });
  140. }
  141. var API = {
  142. /**
  143. * Check whether the API should be enabled or not.
  144. * @returns {boolean}
  145. */
  146. isEnabled: function () {
  147. var hash = location.hash;
  148. if (hash && hash.indexOf("external") > -1 && window.postMessage)
  149. return true;
  150. return false;
  151. },
  152. /**
  153. * Initializes the APIConnector. Setups message event listeners that will
  154. * receive information from external applications that embed Jitsi Meet.
  155. * It also sends a message to the external application that APIConnector
  156. * is initialized.
  157. */
  158. init: function () {
  159. initCommands();
  160. if (window.addEventListener) {
  161. window.addEventListener('message',
  162. processMessage, false);
  163. }
  164. else {
  165. window.attachEvent('onmessage', processMessage);
  166. }
  167. sendMessage({type: "system", loaded: true});
  168. setupListeners();
  169. },
  170. /**
  171. * Checks whether the event is enabled ot not.
  172. * @param name the name of the event.
  173. * @returns {*}
  174. */
  175. isEventEnabled: function (name) {
  176. return events[name];
  177. },
  178. /**
  179. * Sends event object to the external application that has been subscribed
  180. * for that event.
  181. * @param name the name event
  182. * @param object data associated with the event
  183. */
  184. triggerEvent: function (name, object) {
  185. if(this.isEnabled() && this.isEventEnabled(name))
  186. sendMessage({
  187. type: "event", action: "result", event: name, result: object});
  188. },
  189. /**
  190. * Removes the listeners.
  191. */
  192. dispose: function () {
  193. if(window.removeEventListener) {
  194. window.removeEventListener("message",
  195. processMessage, false);
  196. }
  197. else {
  198. window.detachEvent('onmessage', processMessage);
  199. }
  200. }
  201. };
  202. module.exports = API;