You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

external_api.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * Implements API class that embeds Jitsi Meet in external applications.
  3. */
  4. var JitsiMeetExternalAPI = (function()
  5. {
  6. /**
  7. * The minimum width for the Jitsi Meet frame
  8. * @type {number}
  9. */
  10. var MIN_WIDTH = 790;
  11. /**
  12. * The minimum height for the Jitsi Meet frame
  13. * @type {number}
  14. */
  15. var MIN_HEIGHT = 300;
  16. /**
  17. * Constructs new API instance. Creates iframe element that loads
  18. * Jitsi Meet.
  19. * @param domain the domain name of the server that hosts the conference
  20. * @param room_name the name of the room to join
  21. * @param width width of the iframe
  22. * @param height height of the iframe
  23. * @param parent_node the node that will contain the iframe
  24. * @constructor
  25. */
  26. function JitsiMeetExternalAPI(domain, room_name, width, height, parentNode) {
  27. if(!width || width < MIN_WIDTH)
  28. width = MIN_WIDTH;
  29. if(!height || height < MIN_HEIGHT)
  30. height = MIN_HEIGHT;
  31. this.parentNode = null;
  32. if (parentNode) {
  33. this.parentNode = parentNode;
  34. } else {
  35. var scriptTag = document.scripts[document.scripts.length - 1];
  36. this.parentNode = scriptTag.parentNode;
  37. }
  38. this.iframeHolder =
  39. this.parentNode.appendChild(document.createElement("div"));
  40. this.iframeHolder.id = "jitsiConference" + JitsiMeetExternalAPI.id;
  41. this.iframeHolder.style.width = width + "px";
  42. this.iframeHolder.style.height = height + "px";
  43. this.frameName = "jitsiConferenceFrame" + JitsiMeetExternalAPI.id;
  44. this.url = "//" + domain + "/";
  45. if(room_name)
  46. this.url += room_name;
  47. this.url += "#external=true";
  48. JitsiMeetExternalAPI.id++;
  49. this.frame = document.createElement("iframe");
  50. this.frame.src = this.url;
  51. this.frame.name = this.frameName;
  52. this.frame.id = this.frameName;
  53. this.frame.width = "100%";
  54. this.frame.height = "100%";
  55. this.frame.setAttribute("allowFullScreen","true");
  56. this.frame = this.iframeHolder.appendChild(this.frame);
  57. this.frameLoaded = false;
  58. this.initialCommands = [];
  59. this.eventHandlers = {};
  60. this.initListeners();
  61. }
  62. /**
  63. * Last id of api object
  64. * @type {number}
  65. */
  66. JitsiMeetExternalAPI.id = 0;
  67. /**
  68. * Sends the passed object to Jitsi Meet
  69. * @param object the object to be sent
  70. */
  71. JitsiMeetExternalAPI.prototype.sendMessage = function(object) {
  72. if (this.frameLoaded) {
  73. this.frame.contentWindow.postMessage(
  74. JSON.stringify(object), this.frame.src);
  75. }
  76. else {
  77. this.initialCommands.push(object);
  78. }
  79. };
  80. /**
  81. * Executes command. The available commands are:
  82. * displayName - sets the display name of the local participant to the value
  83. * passed in the arguments array.
  84. * muteAudio - mutes / unmutes audio with no arguments
  85. * muteVideo - mutes / unmutes video with no arguments
  86. * filmStrip - hides / shows the film strip with no arguments
  87. * If the command doesn't require any arguments the parameter should be set
  88. * to empty array or it may be omitted.
  89. * @param name the name of the command
  90. * @param arguments array of arguments
  91. */
  92. JitsiMeetExternalAPI.prototype.executeCommand = function(name,
  93. argumentsList) {
  94. var argumentsArray = argumentsList;
  95. if (!argumentsArray)
  96. argumentsArray = [];
  97. var object = {type: "command", action: "execute"};
  98. object[name] = argumentsArray;
  99. this.sendMessage(object);
  100. };
  101. /**
  102. * Executes commands. The available commands are:
  103. * displayName - sets the display name of the local participant to the value
  104. * passed in the arguments array.
  105. * muteAudio - mutes / unmutes audio with no arguments
  106. * muteVideo - mutes / unmutes video with no arguments
  107. * filmStrip - hides / shows the film strip with no arguments
  108. * @param object the object with commands to be executed. The keys of the
  109. * object are the commands that will be executed and the values are the
  110. * arguments for the command.
  111. */
  112. JitsiMeetExternalAPI.prototype.executeCommands = function (object) {
  113. object.type = "command";
  114. object.action = "execute";
  115. this.sendMessage(object);
  116. };
  117. /**
  118. * Adds event listeners to Meet Jitsi. The object key should be the name of
  119. * the event and value - the listener.
  120. * Currently we support the following
  121. * events:
  122. * incomingMessage - receives event notifications about incoming
  123. * messages. The listener will receive object with the following structure:
  124. * {{
  125. * "from": from,//JID of the user that sent the message
  126. * "nick": nick,//the nickname of the user that sent the message
  127. * "message": txt//the text of the message
  128. * }}
  129. * outgoingMessage - receives event notifications about outgoing
  130. * messages. The listener will receive object with the following structure:
  131. * {{
  132. * "message": txt//the text of the message
  133. * }}
  134. * displayNameChanged - receives event notifications about display name
  135. * change. The listener will receive object with the following structure:
  136. * {{
  137. * jid: jid,//the JID of the participant that changed his display name
  138. * displayname: displayName //the new display name
  139. * }}
  140. * participantJoined - receives event notifications about new participant.
  141. * The listener will receive object with the following structure:
  142. * {{
  143. * jid: jid //the jid of the participant
  144. * }}
  145. * participantLeft - receives event notifications about participant that left room.
  146. * The listener will receive object with the following structure:
  147. * {{
  148. * jid: jid //the jid of the participant
  149. * }}
  150. * @param object
  151. */
  152. JitsiMeetExternalAPI.prototype.addEventListeners
  153. = function (object) {
  154. var message = {type: "event", action: "add", events: []};
  155. for(var i in object)
  156. {
  157. message.events.push(i);
  158. this.eventHandlers[i] = object[i];
  159. }
  160. this.sendMessage(message);
  161. };
  162. /**
  163. * Adds event listeners to Meet Jitsi. Currently we support the following
  164. * events:
  165. * incomingMessage - receives event notifications about incoming
  166. * messages. The listener will receive object with the following structure:
  167. * {{
  168. * "from": from,//JID of the user that sent the message
  169. * "nick": nick,//the nickname of the user that sent the message
  170. * "message": txt//the text of the message
  171. * }}
  172. * outgoingMessage - receives event notifications about outgoing
  173. * messages. The listener will receive object with the following structure:
  174. * {{
  175. * "message": txt//the text of the message
  176. * }}
  177. * displayNameChanged - receives event notifications about display name
  178. * change. The listener will receive object with the following structure:
  179. * {{
  180. * jid: jid,//the JID of the participant that changed his display name
  181. * displayname: displayName //the new display name
  182. * }}
  183. * participantJoined - receives event notifications about new participant.
  184. * The listener will receive object with the following structure:
  185. * {{
  186. * jid: jid //the jid of the participant
  187. * }}
  188. * participantLeft - receives event notifications about participant that left room.
  189. * The listener will receive object with the following structure:
  190. * {{
  191. * jid: jid //the jid of the participant
  192. * }}
  193. * @param event the name of the event
  194. * @param listener the listener
  195. */
  196. JitsiMeetExternalAPI.prototype.addEventListener
  197. = function (event, listener) {
  198. var message = {type: "event", action: "add", events: [event]};
  199. this.eventHandlers[event] = listener;
  200. this.sendMessage(message);
  201. };
  202. /**
  203. * Removes event listener.
  204. * @param event the name of the event.
  205. */
  206. JitsiMeetExternalAPI.prototype.removeEventListener
  207. = function (event) {
  208. if(!this.eventHandlers[event])
  209. {
  210. console.error("The event " + event + " is not registered.");
  211. return;
  212. }
  213. var message = {type: "event", action: "remove", events: [event]};
  214. delete this.eventHandlers[event];
  215. this.sendMessage(message);
  216. };
  217. /**
  218. * Removes event listeners.
  219. * @param events array with the names of the events.
  220. */
  221. JitsiMeetExternalAPI.prototype.removeEventListeners
  222. = function (events) {
  223. var eventsArray = [];
  224. for(var i = 0; i < events.length; i++)
  225. {
  226. var event = events[i];
  227. if(!this.eventHandlers[event])
  228. {
  229. console.error("The event " + event + " is not registered.");
  230. continue;
  231. }
  232. delete this.eventHandlers[event];
  233. eventsArray.push(event);
  234. }
  235. if(eventsArray.length > 0)
  236. {
  237. this.sendMessage(
  238. {type: "event", action: "remove", events: eventsArray});
  239. }
  240. };
  241. /**
  242. * Processes message events sent from Jitsi Meet
  243. * @param event the event
  244. */
  245. JitsiMeetExternalAPI.prototype.processMessage = function(event) {
  246. var message;
  247. try {
  248. message = JSON.parse(event.data);
  249. } catch (e) {}
  250. if(!message.type) {
  251. console.error("Message without type is received.");
  252. return;
  253. }
  254. switch (message.type) {
  255. case "system":
  256. if(message.loaded) {
  257. this.onFrameLoaded();
  258. }
  259. break;
  260. case "event":
  261. if(message.action != "result" ||
  262. !message.event || !this.eventHandlers[message.event]) {
  263. console.warn("The received event cannot be parsed.");
  264. return;
  265. }
  266. this.eventHandlers[message.event](message.result);
  267. break;
  268. default :
  269. console.error("Unknown message type.");
  270. return;
  271. }
  272. };
  273. /**
  274. * That method is called when the Jitsi Meet is loaded. Executes saved
  275. * commands that are send before the frame was loaded.
  276. */
  277. JitsiMeetExternalAPI.prototype.onFrameLoaded = function () {
  278. this.frameLoaded = true;
  279. for (var i = 0; i < this.initialCommands.length; i++) {
  280. this.sendMessage(this.initialCommands[i]);
  281. }
  282. this.initialCommands = null;
  283. };
  284. /**
  285. * Setups the listener for message events from Jitsi Meet.
  286. */
  287. JitsiMeetExternalAPI.prototype.initListeners = function () {
  288. var self = this;
  289. this.eventListener = function (event) {
  290. self.processMessage(event);
  291. };
  292. if (window.addEventListener) {
  293. window.addEventListener('message',
  294. this.eventListener, false);
  295. }
  296. else {
  297. window.attachEvent('onmessage', this.eventListener);
  298. }
  299. };
  300. /**
  301. * Removes the listeners and removes the Jitsi Meet frame.
  302. */
  303. JitsiMeetExternalAPI.prototype.dispose = function () {
  304. if (window.removeEventListener) {
  305. window.removeEventListener('message',
  306. this.eventListener, false);
  307. }
  308. else {
  309. window.detachEvent('onmessage',
  310. this.eventListener);
  311. }
  312. var frame = document.getElementById(this.frameName);
  313. if(frame)
  314. frame.src = 'about:blank';
  315. var self = this;
  316. window.setTimeout(function () {
  317. self.iframeHolder.removeChild(self.frame);
  318. self.iframeHolder.parentNode.removeChild(self.iframeHolder);
  319. }, 10);
  320. };
  321. return JitsiMeetExternalAPI;
  322. })();