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

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