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

external_api.js 13KB

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