Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

external_api.js 13KB

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