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

API.js 6.1KB

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