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

external_api.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. const logger = require("jitsi-meet-logger").getLogger(__filename);
  2. import postisInit from "postis";
  3. import EventEmitter from "events";
  4. /**
  5. * The minimum width for the Jitsi Meet frame
  6. * @type {number}
  7. */
  8. const MIN_WIDTH = 790;
  9. /**
  10. * The minimum height for the Jitsi Meet frame
  11. * @type {number}
  12. */
  13. const MIN_HEIGHT = 300;
  14. /**
  15. * Last id of api object
  16. * @type {number}
  17. */
  18. let id = 0;
  19. /**
  20. * Maps the names of the commands expected by the API with the name of the
  21. * commands expected by jitsi-meet
  22. */
  23. const commands = {
  24. "displayName": "display-name",
  25. "toggleAudio": "toggle-audio",
  26. "toggleVideo": "toggle-video",
  27. "toggleFilmStrip": "toggle-film-strip",
  28. "toggleChat": "toggle-chat",
  29. "toggleContactList": "toggle-contact-list",
  30. "toggleShareScreen": "toggle-share-screen",
  31. "hangup": "video-hangup",
  32. "email": "email",
  33. "avatarUrl": "avatar-url"
  34. };
  35. /**
  36. * Maps the names of the events expected by the API with the name of the
  37. * events expected by jitsi-meet
  38. */
  39. const events = {
  40. "incomingMessage": "incoming-message",
  41. "outgoingMessage": "outgoing-message",
  42. "displayNameChange": "display-name-change",
  43. "participantJoined": "participant-joined",
  44. "participantLeft": "participant-left",
  45. "videoConferenceJoined": "video-conference-joined",
  46. "videoConferenceLeft": "video-conference-left",
  47. "readyToClose": "video-ready-to-close"
  48. };
  49. /**
  50. * Sends the passed object to Jitsi Meet
  51. * @param postis {Postis object} the postis instance that is going to be used
  52. * to send the message
  53. * @param object the object to be sent
  54. * - method {sting}
  55. * - params {object}
  56. */
  57. function sendMessage(postis, object) {
  58. postis.send(object);
  59. }
  60. /**
  61. * Adds given number to the numberOfParticipants property of given APIInstance.
  62. * @param {JitsiMeetExternalAPI} APIInstance the instance of the
  63. * JitsiMeetExternalAPI
  64. * @param {int} number - the number of participants to be added to
  65. * numberOfParticipants property (this parameter can be negative number if the
  66. * numberOfParticipants should be decreased).
  67. */
  68. function changeParticipantNumber(APIInstance, number) {
  69. APIInstance.numberOfParticipants += number;
  70. }
  71. /**
  72. * Generates array with URL params based on the passed config object that will
  73. * be used for the Jitsi Meet URL generation.
  74. *
  75. * @param config {object} the config object.
  76. * @returns {Array<string>} the array with URL param strings.
  77. */
  78. function configToURLParamsArray(config) {
  79. const params = [];
  80. for (const key in config) {
  81. try {
  82. params.push(key + '='
  83. + encodeURIComponent(JSON.stringify(config[key])));
  84. } catch (e) {
  85. console.warn(`Error encoding ${key}: ${e}`);
  86. }
  87. }
  88. return params;
  89. }
  90. /**
  91. * The IFrame API interface class.
  92. */
  93. class JitsiMeetExternalAPI extends EventEmitter {
  94. /**
  95. * Constructs new API instance. Creates iframe element that loads
  96. * Jitsi Meet.
  97. * @param domain the domain name of the server that hosts the conference
  98. * @param room_name the name of the room to join
  99. * @param width width of the iframe
  100. * @param height height of the iframe
  101. * @param parent_node the node that will contain the iframe
  102. * @param configOverwrite object containing configuration options defined in
  103. * config.js to be overridden.
  104. * @param interfaceConfigOverwrite object containing configuration options
  105. * defined in interface_config.js to be overridden.
  106. * @param noSsl if the value is true https won't be used
  107. * @param {string} [jwt] the JWT token if needed by jitsi-meet for
  108. * authentication.
  109. * @constructor
  110. */
  111. constructor(domain, room_name, width, height, parentNode,
  112. configOverwrite, interfaceConfigOverwrite, noSsl, jwt) {
  113. super();
  114. if (!width || width < MIN_WIDTH) {
  115. width = MIN_WIDTH;
  116. }
  117. if (!height || height < MIN_HEIGHT) {
  118. height = MIN_HEIGHT;
  119. }
  120. this.parentNode = null;
  121. if (parentNode) {
  122. this.parentNode = parentNode;
  123. } else {
  124. var scriptTag = document.scripts[document.scripts.length - 1];
  125. this.parentNode = scriptTag.parentNode;
  126. }
  127. this.iframeHolder =
  128. this.parentNode.appendChild(document.createElement("div"));
  129. this.iframeHolder.id = "jitsiConference" + id;
  130. if (width) {
  131. this.iframeHolder.style.width = width + "px";
  132. }
  133. if (height) {
  134. this.iframeHolder.style.height = height + "px";
  135. }
  136. this.frameName = "jitsiConferenceFrame" + id;
  137. this.url = (noSsl) ? "http" : "https" +"://" + domain + "/";
  138. if (room_name) {
  139. this.url += room_name;
  140. }
  141. if (jwt) {
  142. this.url += '?jwt=' + jwt;
  143. }
  144. this.url += "#jitsi_meet_external_api_id=" + id;
  145. const configURLParams = configToURLParamsArray(configOverwrite);
  146. if (configURLParams.length) {
  147. this.url += '&config.' + configURLParams.join('&config.');
  148. }
  149. const interfaceConfigURLParams
  150. = configToURLParamsArray(interfaceConfigOverwrite);
  151. if (interfaceConfigURLParams.length) {
  152. this.url += '&interfaceConfig.'
  153. + interfaceConfigURLParams.join('&interfaceConfig.');
  154. }
  155. this.frame = document.createElement("iframe");
  156. this.frame.src = this.url;
  157. this.frame.name = this.frameName;
  158. this.frame.id = this.frameName;
  159. this.frame.width = "100%";
  160. this.frame.height = "100%";
  161. this.frame.setAttribute("allowFullScreen","true");
  162. this.frame = this.iframeHolder.appendChild(this.frame);
  163. this.postis = postisInit({
  164. window: this.frame.contentWindow,
  165. scope: "jitsi_meet_external_api_" + id
  166. });
  167. this.eventHandlers = {};
  168. // Map<{string} event_name, {boolean} postis_listener_added>
  169. this.postisListeners = {};
  170. this.numberOfParticipants = 1;
  171. this._setupListeners();
  172. id++;
  173. }
  174. /**
  175. * Executes command. The available commands are:
  176. * displayName - sets the display name of the local participant to the value
  177. * passed in the arguments array.
  178. * toggleAudio - mutes / unmutes audio with no arguments
  179. * toggleVideo - mutes / unmutes video with no arguments
  180. * filmStrip - hides / shows the film strip with no arguments
  181. * If the command doesn't require any arguments the parameter should be set
  182. * to empty array or it may be omitted.
  183. * @param name the name of the command
  184. * @param arguments array of arguments
  185. */
  186. executeCommand(name, ...argumentsList) {
  187. if(!(name in commands)) {
  188. logger.error("Not supported command name.");
  189. return;
  190. }
  191. sendMessage(this.postis, {
  192. method: commands[name],
  193. params: argumentsList
  194. });
  195. }
  196. /**
  197. * Executes commands. The available commands are:
  198. * displayName - sets the display name of the local participant to the value
  199. * passed in the arguments array.
  200. * toggleAudio - mutes / unmutes audio. no arguments
  201. * toggleVideo - mutes / unmutes video. no arguments
  202. * filmStrip - hides / shows the film strip. no arguments
  203. * toggleChat - hides / shows chat. no arguments.
  204. * toggleContactList - hides / shows contact list. no arguments.
  205. * toggleShareScreen - starts / stops screen sharing. no arguments.
  206. * @param object the object with commands to be executed. The keys of the
  207. * object are the commands that will be executed and the values are the
  208. * arguments for the command.
  209. */
  210. executeCommands(object) {
  211. for (var key in object) {
  212. this.executeCommand(key, object[key]);
  213. }
  214. }
  215. /**
  216. * Adds event listeners to Meet Jitsi. The object key should be the name of
  217. * the event and value - the listener.
  218. * Currently we support the following
  219. * events:
  220. * incomingMessage - receives event notifications about incoming
  221. * messages. The listener will receive object with the following structure:
  222. * {{
  223. * "from": from,//JID of the user that sent the message
  224. * "nick": nick,//the nickname of the user that sent the message
  225. * "message": txt//the text of the message
  226. * }}
  227. * outgoingMessage - receives event notifications about outgoing
  228. * messages. The listener will receive object with the following structure:
  229. * {{
  230. * "message": txt//the text of the message
  231. * }}
  232. * displayNameChanged - receives event notifications about display name
  233. * change. The listener will receive object with the following structure:
  234. * {{
  235. * jid: jid,//the JID of the participant that changed his display name
  236. * displayname: displayName //the new display name
  237. * }}
  238. * participantJoined - receives event notifications about new participant.
  239. * The listener will receive object with the following structure:
  240. * {{
  241. * jid: jid //the jid of the participant
  242. * }}
  243. * participantLeft - receives event notifications about the participant that
  244. * left the room.
  245. * The listener will receive object with the following structure:
  246. * {{
  247. * jid: jid //the jid of the participant
  248. * }}
  249. * video-conference-joined - receives event notifications about the local
  250. * user has successfully joined the video conference.
  251. * The listener will receive object with the following structure:
  252. * {{
  253. * roomName: room //the room name of the conference
  254. * }}
  255. * video-conference-left - receives event notifications about the local user
  256. * has left the video conference.
  257. * The listener will receive object with the following structure:
  258. * {{
  259. * roomName: room //the room name of the conference
  260. * }}
  261. * readyToClose - all hangup operations are completed and Jitsi Meet is
  262. * ready to be disposed.
  263. * @param object
  264. *
  265. * NOTE: This method is not removed for backward comatability purposes.
  266. */
  267. addEventListeners(object) {
  268. for (var i in object) {
  269. this.addEventListener(i, object[i]);
  270. }
  271. }
  272. /**
  273. * Adds event listeners to Meet Jitsi. Currently we support the following
  274. * events:
  275. * incomingMessage - receives event notifications about incoming
  276. * messages. The listener will receive object with the following structure:
  277. * {{
  278. * "from": from,//JID of the user that sent the message
  279. * "nick": nick,//the nickname of the user that sent the message
  280. * "message": txt//the text of the message
  281. * }}
  282. * outgoingMessage - receives event notifications about outgoing
  283. * messages. The listener will receive object with the following structure:
  284. * {{
  285. * "message": txt//the text of the message
  286. * }}
  287. * displayNameChanged - receives event notifications about display name
  288. * change. The listener will receive object with the following structure:
  289. * {{
  290. * jid: jid,//the JID of the participant that changed his display name
  291. * displayname: displayName //the new display name
  292. * }}
  293. * participantJoined - receives event notifications about new participant.
  294. * The listener will receive object with the following structure:
  295. * {{
  296. * jid: jid //the jid of the participant
  297. * }}
  298. * participantLeft - receives event notifications about participant the that
  299. * left the room.
  300. * The listener will receive object with the following structure:
  301. * {{
  302. * jid: jid //the jid of the participant
  303. * }}
  304. * video-conference-joined - receives event notifications fired when the
  305. * local user has joined the video conference.
  306. * The listener will receive object with the following structure:
  307. * {{
  308. * roomName: room //the room name of the conference
  309. * }}
  310. * video-conference-left - receives event notifications fired when the local
  311. * user has joined the video conference.
  312. * The listener will receive object with the following structure:
  313. * {{
  314. * roomName: room //the room name of the conference
  315. * }}
  316. * @param event the name of the event
  317. * @param listener the listener
  318. *
  319. * NOTE: This method is not removed for backward comatability purposes.
  320. */
  321. addEventListener(event, listener) {
  322. this.on(event, listener);
  323. }
  324. /**
  325. * Removes event listener.
  326. * @param event the name of the event.
  327. * NOTE: This method is not removed for backward comatability purposes.
  328. */
  329. removeEventListener(event) {
  330. this.removeListeners(event);
  331. }
  332. /**
  333. * Removes event listeners.
  334. * @param events array with the names of the events.
  335. * NOTE: This method is not removed for backward comatability purposes.
  336. */
  337. removeEventListeners(events) {
  338. for(var i = 0; i < events.length; i++) {
  339. this.removeEventListener(events[i]);
  340. }
  341. }
  342. /**
  343. * Returns the number of participants in the conference.
  344. * NOTE: the local participant is included.
  345. * @returns {int} the number of participants in the conference.
  346. */
  347. getNumberOfParticipants() {
  348. return this.numberOfParticipants;
  349. }
  350. /**
  351. * Setups listeners that are used internally for JitsiMeetExternalAPI.
  352. */
  353. _setupListeners() {
  354. this.postis.listen("participant-joined",
  355. changeParticipantNumber.bind(null, this, 1));
  356. this.postis.listen("participant-left",
  357. changeParticipantNumber.bind(null, this, -1));
  358. for (const eventName in events) {
  359. const postisMethod = events[eventName];
  360. this.postis.listen(postisMethod,
  361. (...args) => this.emit(eventName, ...args));
  362. }
  363. }
  364. /**
  365. * Removes the listeners and removes the Jitsi Meet frame.
  366. */
  367. dispose() {
  368. this.postis.destroy();
  369. var frame = document.getElementById(this.frameName);
  370. if(frame)
  371. frame.src = 'about:blank';
  372. window.setTimeout( () => {
  373. this.iframeHolder.removeChild(this.frame);
  374. this.iframeHolder.parentNode.removeChild(this.iframeHolder);
  375. }, 10);
  376. }
  377. }
  378. module.exports = JitsiMeetExternalAPI;