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

API.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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,
  123. function (from, nick, txt, myjid, stamp) {
  124. if (from != myjid)
  125. API.triggerEvent("incomingMessage",
  126. {"from": from, "nick": nick, "message": txt, "stamp": stamp});
  127. });
  128. APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_LEFT, function (jid) {
  129. API.triggerEvent("participantLeft", {jid: jid});
  130. });
  131. APP.xmpp.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  132. function (jid, newDisplayName) {
  133. var name = displayName[jid];
  134. if(!name || name != newDisplayName) {
  135. API.triggerEvent("displayNameChange",
  136. {jid: jid, displayname: newDisplayName});
  137. displayName[jid] = newDisplayName;
  138. }
  139. });
  140. APP.xmpp.addListener(XMPPEvents.SENDING_CHAT_MESSAGE, function (body) {
  141. APP.API.triggerEvent("outgoingMessage", {"message": body});
  142. });
  143. }
  144. var API = {
  145. /**
  146. * Check whether the API should be enabled or not.
  147. * @returns {boolean}
  148. */
  149. isEnabled: function () {
  150. var hash = location.hash;
  151. if (hash && hash.indexOf("external") > -1 && window.postMessage)
  152. return true;
  153. return false;
  154. },
  155. /**
  156. * Initializes the APIConnector. Setups message event listeners that will
  157. * receive information from external applications that embed Jitsi Meet.
  158. * It also sends a message to the external application that APIConnector
  159. * is initialized.
  160. */
  161. init: function () {
  162. initCommands();
  163. if (window.addEventListener) {
  164. window.addEventListener('message',
  165. processMessage, false);
  166. }
  167. else {
  168. window.attachEvent('onmessage', processMessage);
  169. }
  170. sendMessage({type: "system", loaded: true});
  171. setupListeners();
  172. },
  173. /**
  174. * Checks whether the event is enabled ot not.
  175. * @param name the name of the event.
  176. * @returns {*}
  177. */
  178. isEventEnabled: function (name) {
  179. return events[name];
  180. },
  181. /**
  182. * Sends event object to the external application that has been subscribed
  183. * for that event.
  184. * @param name the name event
  185. * @param object data associated with the event
  186. */
  187. triggerEvent: function (name, object) {
  188. if(this.isEnabled() && this.isEventEnabled(name))
  189. sendMessage({
  190. type: "event", action: "result", event: name, result: object});
  191. },
  192. /**
  193. * Removes the listeners.
  194. */
  195. dispose: function () {
  196. if(window.removeEventListener) {
  197. window.removeEventListener("message",
  198. processMessage, false);
  199. }
  200. else {
  201. window.detachEvent('onmessage', processMessage);
  202. }
  203. }
  204. };
  205. module.exports = API;