Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

external_api.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Implements API class that embeds Jitsi Meet in external applications.
  18. */
  19. var JitsiMeetExternalAPI = (function()
  20. {
  21. /**
  22. * The minimum width for the Jitsi Meet frame
  23. * @type {number}
  24. */
  25. var MIN_WIDTH = 790;
  26. /**
  27. * The minimum height for the Jitsi Meet frame
  28. * @type {number}
  29. */
  30. var MIN_HEIGHT = 300;
  31. /**
  32. * Constructs new API instance. Creates iframe element that loads
  33. * Jitsi Meet.
  34. * @param domain the domain name of the server that hosts the conference
  35. * @param room_name the name of the room to join
  36. * @param width width of the iframe
  37. * @param height height of the iframe
  38. * @param parent_node the node that will contain the iframe
  39. * @constructor
  40. */
  41. function JitsiMeetExternalAPI(domain, room_name, width, height, parent_node)
  42. {
  43. this.parentNode = null;
  44. if(parent_node)
  45. {
  46. this.parentNode = parent_node;
  47. }
  48. else
  49. {
  50. var scriptTag = document.scripts[document.scripts.length - 1];
  51. this.parentNode = scriptTag.parentNode;
  52. }
  53. this.iframeHolder =
  54. this.parentNode.appendChild(document.createElement("div"));
  55. this.iframeHolder.id = "jitsiConference" + JitsiMeetExternalAPI.id;
  56. if(width < MIN_WIDTH)
  57. width = MIN_WIDTH;
  58. if(height < MIN_HEIGHT)
  59. height = MIN_HEIGHT;
  60. this.iframeHolder.style.width = width + "px";
  61. this.iframeHolder.style.height = height + "px";
  62. this.frameName = "jitsiConferenceFrame" + JitsiMeetExternalAPI.id;
  63. this.url = "//" + domain + "/";
  64. if(room_name)
  65. this.url += room_name;
  66. this.url += "#external";
  67. JitsiMeetExternalAPI.id++;
  68. this.frame = document.createElement("iframe");
  69. this.frame.src = this.url;
  70. this.frame.name = this.frameName;
  71. this.frame.id = this.frameName;
  72. this.frame.width = "100%";
  73. this.frame.height = "100%";
  74. this.frame.setAttribute("allowFullScreen","true");
  75. this.frame = this.iframeHolder.appendChild(this.frame);
  76. this.frameLoaded = false;
  77. this.initialCommands = [];
  78. this.eventHandlers = {};
  79. this.initListeners();
  80. }
  81. /**
  82. * Last id of api object
  83. * @type {number}
  84. */
  85. JitsiMeetExternalAPI.id = 0;
  86. /**
  87. * Sends the passed object to Jitsi Meet
  88. * @param object the object to be sent
  89. */
  90. JitsiMeetExternalAPI.prototype.sendMessage = function(object)
  91. {
  92. if(this.frameLoaded)
  93. {
  94. this.frame.contentWindow.postMessage(
  95. JSON.stringify(object), this.frame.src);
  96. }
  97. else
  98. {
  99. this.initialCommands.push(object);
  100. }
  101. };
  102. /**
  103. * Executes command. The available commands are:
  104. * displayName - sets the display name of the local participant to the value
  105. * passed in the arguments array.
  106. * muteAudio - mutes / unmutes audio with no arguments
  107. * muteVideo - mutes / unmutes video with no arguments
  108. * filmStrip - hides / shows the film strip with no arguments
  109. * If the command doesn't require any arguments the parameter should be set
  110. * to empty array or it may be omitted.
  111. * @param name the name of the command
  112. * @param arguments array of arguments
  113. */
  114. JitsiMeetExternalAPI.prototype.executeCommand = function(name,
  115. argumentsList)
  116. {
  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. * muteAudio - mutes / unmutes audio with no arguments
  129. * muteVideo - 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 the
  142. * 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 participant that left room.
  169. * The listener will receive object with the following structure:
  170. * {{
  171. * jid: jid //the jid of the participant
  172. * }}
  173. * @param object
  174. */
  175. JitsiMeetExternalAPI.prototype.addEventListeners
  176. = function (object)
  177. {
  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 that left room.
  213. * The listener will receive object with the following structure:
  214. * {{
  215. * jid: jid //the jid of the participant
  216. * }}
  217. * @param event the name of the event
  218. * @param listener the listener
  219. */
  220. JitsiMeetExternalAPI.prototype.addEventListener
  221. = function (event, listener)
  222. {
  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. {
  234. if(!this.eventHandlers[event])
  235. {
  236. console.error("The event " + event + " is not registered.");
  237. return;
  238. }
  239. var message = {type: "event", action: "remove", events: [event]};
  240. delete this.eventHandlers[event];
  241. this.sendMessage(message);
  242. };
  243. /**
  244. * Removes event listeners.
  245. * @param events array with the names of the events.
  246. */
  247. JitsiMeetExternalAPI.prototype.removeEventListeners
  248. = function (events)
  249. {
  250. var eventsArray = [];
  251. for(var i = 0; i < events.length; i++)
  252. {
  253. var event = events[i];
  254. if(!this.eventHandlers[event])
  255. {
  256. console.error("The event " + event + " is not registered.");
  257. continue;
  258. }
  259. delete this.eventHandlers[event];
  260. eventsArray.push(event);
  261. }
  262. if(eventsArray.length > 0)
  263. {
  264. this.sendMessage(
  265. {type: "event", action: "remove", events: eventsArray});
  266. }
  267. };
  268. /**
  269. * Processes message events sent from Jitsi Meet
  270. * @param event the event
  271. */
  272. JitsiMeetExternalAPI.prototype.processMessage = function(event)
  273. {
  274. var message;
  275. try {
  276. message = JSON.parse(event.data);
  277. } catch (e) {}
  278. if(!message.type) {
  279. console.error("Message without type is received.");
  280. return;
  281. }
  282. switch (message.type)
  283. {
  284. case "system":
  285. if(message.loaded)
  286. {
  287. this.onFrameLoaded();
  288. }
  289. break;
  290. case "event":
  291. if(message.action != "result" ||
  292. !message.event || !this.eventHandlers[message.event])
  293. {
  294. console.warn("The received event cannot be parsed.");
  295. return;
  296. }
  297. this.eventHandlers[message.event](message.result);
  298. break;
  299. default :
  300. console.error("Unknown message type.");
  301. return;
  302. }
  303. };
  304. /**
  305. * That method is called when the Jitsi Meet is loaded. Executes saved
  306. * commands that are send before the frame was loaded.
  307. */
  308. JitsiMeetExternalAPI.prototype.onFrameLoaded = function () {
  309. this.frameLoaded = true;
  310. for (var i = 0; i < this.initialCommands.length; i++)
  311. {
  312. this.sendMessage(this.initialCommands[i]);
  313. }
  314. this.initialCommands = null;
  315. };
  316. /**
  317. * Setups the listener for message events from Jitsi Meet.
  318. */
  319. JitsiMeetExternalAPI.prototype.initListeners = function () {
  320. var self = this;
  321. this.eventListener = function (event) {
  322. self.processMessage(event);
  323. };
  324. if (window.addEventListener)
  325. {
  326. window.addEventListener('message',
  327. this.eventListener, false);
  328. }
  329. else
  330. {
  331. window.attachEvent('onmessage', this.eventListener);
  332. }
  333. };
  334. /**
  335. * Removes the listeners and removes the Jitsi Meet frame.
  336. */
  337. JitsiMeetExternalAPI.prototype.dispose = function () {
  338. if (window.removeEventListener)
  339. {
  340. window.removeEventListener('message',
  341. this.eventListener, false);
  342. }
  343. else
  344. {
  345. window.detachEvent('onmessage',
  346. this.eventListener);
  347. }
  348. var frame = document.getElementById(this.frameName);
  349. if(frame)
  350. frame.src = 'about:blank';
  351. var self = this;
  352. window.setTimeout(function () {
  353. self.iframeHolder.removeChild(self.frame);
  354. self.iframeHolder.parentNode.removeChild(self.iframeHolder);
  355. }, 10);
  356. };
  357. return JitsiMeetExternalAPI;
  358. })();