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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* global $, JitsiMeetJS, config, Promise */
  2. /* application specific logic */
  3. import "babel-polyfill";
  4. require("jquery");
  5. require("jquery-ui");
  6. require("strophe");
  7. require("strophe-disco");
  8. require("strophe-caps");
  9. require("tooltip");
  10. require("popover");
  11. window.toastr = require("toastr");
  12. require("jQuery-Impromptu");
  13. require("autosize");
  14. var CQEvents = require('./service/connectionquality/CQEvents');
  15. var UIEvents = require('./service/UI/UIEvents');
  16. var Commands = {
  17. CONNECTION_QUALITY: "connectionQuality",
  18. EMAIL: "email"
  19. };
  20. var APP = {
  21. init: function () {
  22. JitsiMeetJS.init();
  23. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
  24. this.conference = {
  25. localId: undefined,
  26. isModerator: false,
  27. membersCount: 0,
  28. audioMuted: false,
  29. videoMuted: false,
  30. isLocalId: function (id) {
  31. return this.localId === id;
  32. },
  33. muteAudio: function (mute) {
  34. APP.UI.eventEmitter.emit(UIEvents.AUDIO_MUTED, mute);
  35. },
  36. toggleAudioMuted: function () {
  37. this.muteAudio(!this.audioMuted);
  38. },
  39. muteVideo: function (mute) {
  40. APP.UI.eventEmitter.emit(UIEvents.VIDEO_MUTED, mute);
  41. },
  42. toggleVideoMuted: function () {
  43. this.muteVideo(!this.videoMuted);
  44. }
  45. };
  46. this.UI = require("./modules/UI/UI");
  47. this.API = require("./modules/API/API");
  48. this.connectionquality =
  49. require("./modules/connectionquality/connectionquality");
  50. this.statistics = require("./modules/statistics/statistics");
  51. this.desktopsharing =
  52. require("./modules/desktopsharing/desktopsharing");
  53. this.keyboardshortcut =
  54. require("./modules/keyboardshortcut/keyboardshortcut");
  55. this.translation = require("./modules/translation/translation");
  56. this.settings = require("./modules/settings/Settings");
  57. this.configFetch = require("./modules/config/HttpConfigFetch");
  58. }
  59. };
  60. var ConnectionEvents = JitsiMeetJS.events.connection;
  61. var ConnectionErrors = JitsiMeetJS.errors.connection;
  62. function connect() {
  63. var connection = new JitsiMeetJS.JitsiConnection(null, null, {
  64. hosts: config.hosts,
  65. bosh: config.bosh,
  66. clientNode: config.clientNode
  67. });
  68. return new Promise(function (resolve, reject) {
  69. var handlers = {};
  70. var unsubscribe = function () {
  71. Object.keys(handlers).forEach(function (event) {
  72. connection.removeEventListener(event, handlers[event]);
  73. });
  74. };
  75. handlers[ConnectionEvents.CONNECTION_ESTABLISHED] = function () {
  76. console.log('CONNECTED');
  77. unsubscribe();
  78. resolve(connection);
  79. };
  80. var listenForFailure = function (event) {
  81. handlers[event] = function () {
  82. // convert arguments to array
  83. var args = Array.prototype.slice.call(arguments);
  84. args.unshift(event);
  85. // [event, ...params]
  86. console.error('CONNECTION FAILED:', args);
  87. unsubscribe();
  88. reject(args);
  89. };
  90. };
  91. listenForFailure(ConnectionEvents.CONNECTION_FAILED);
  92. listenForFailure(ConnectionErrors.PASSWORD_REQUIRED);
  93. listenForFailure(ConnectionErrors.CONNECTION_ERROR);
  94. listenForFailure(ConnectionErrors.OTHER_ERRORS);
  95. // install event listeners
  96. Object.keys(handlers).forEach(function (event) {
  97. connection.addEventListener(event, handlers[event]);
  98. });
  99. connection.connect();
  100. }).catch(function (err) {
  101. if (err[0] === ConnectionErrors.PASSWORD_REQUIRED) {
  102. // FIXME ask for password and try again
  103. return connect();
  104. }
  105. console.error('FAILED TO CONNECT', err);
  106. APP.UI.notifyConnectionFailed(err[1]);
  107. throw new Error(err[0]);
  108. });
  109. }
  110. var ConferenceEvents = JitsiMeetJS.events.conference;
  111. var ConferenceErrors = JitsiMeetJS.errors.conference;
  112. function initConference(connection, roomName) {
  113. var room = connection.initJitsiConference(roomName, {
  114. openSctp: config.openSctp,
  115. disableAudioLevels: config.disableAudioLevels
  116. });
  117. var users = {};
  118. var localTracks = [];
  119. APP.conference.localId = room.myUserId();
  120. Object.defineProperty(APP.conference, "membersCount", {
  121. get: function () {
  122. return Object.keys(users).length; // FIXME maybe +1?
  123. }
  124. });
  125. room.on(ConferenceEvents.USER_JOINED, function (id) {
  126. users[id] = {
  127. displayName: undefined,
  128. tracks: []
  129. };
  130. // FIXME email???
  131. APP.UI.addUser(id);
  132. });
  133. room.on(ConferenceEvents.USER_LEFT, function (id) {
  134. delete users[id];
  135. APP.UI.removeUser(id);
  136. });
  137. room.on(ConferenceEvents.TRACK_MUTE_CHANGED, function (track) {
  138. // FIXME handle mute
  139. });
  140. room.on(ConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, function (id, lvl) {
  141. APP.UI.setAudioLevel(id, lvl);
  142. });
  143. APP.UI.addListener(UIEvents.AUDIO_MUTED, function (muted) {
  144. // FIXME mute or unmute
  145. APP.UI.setAudioMuted(muted);
  146. APP.conference.audioMuted = muted;
  147. });
  148. APP.UI.addListener(UIEvents.VIDEO_MUTED, function (muted) {
  149. // FIXME mute or unmute
  150. APP.UI.setVideoMuted(muted);
  151. APP.conference.videoMuted = muted;
  152. });
  153. room.on(ConferenceEvents.IN_LAST_N_CHANGED, function (inLastN) {
  154. if (config.muteLocalVideoIfNotInLastN) {
  155. // TODO mute or unmute if required
  156. // mark video on UI
  157. // APP.UI.markVideoMuted(true/false);
  158. }
  159. });
  160. room.on(ConferenceEvents.LAST_N_ENDPOINTS_CHANGED, function (ids) {
  161. APP.UI.handleLastNEndpoints(ids);
  162. });
  163. room.on(ConferenceEvents.ACTIVE_SPEAKER_CHANGED, function (id) {
  164. APP.UI.markDominantSpiker(id);
  165. });
  166. room.on(ConferenceEvents.CONNECTION_INTERRUPTED, function () {
  167. APP.UI.markVideoInterrupted(true);
  168. });
  169. room.on(ConferenceEvents.CONNECTION_RESTORED, function () {
  170. APP.UI.markVideoInterrupted(false);
  171. });
  172. APP.connectionquality.addListener(
  173. CQEvents.LOCALSTATS_UPDATED,
  174. function (percent, stats) {
  175. APP.UI.updateLocalStats(percent, stats);
  176. // send local stats to other users
  177. room.sendCommand(Commands.CONNECTION_QUALITY, {
  178. value: APP.connectionquality.convertToMUCStats(stats),
  179. attributes: {
  180. id: room.myUserId()
  181. }
  182. });
  183. }
  184. );
  185. APP.connectionquality.addListener(CQEvents.STOP, function () {
  186. APP.UI.hideStats();
  187. room.removeCommand(Commands.CONNECTION_QUALITY);
  188. });
  189. // listen to remote stats
  190. room.addCommandListener(Commands.CONNECTION_QUALITY, function (data) {
  191. APP.connectionquality.updateRemoteStats(data.attributes.id, data.value);
  192. });
  193. APP.connectionquality.addListener(
  194. CQEvents.REMOTESTATS_UPDATED,
  195. function (id, percent, stats) {
  196. APP.UI.updateRemoteStats(id, percent, stats);
  197. }
  198. );
  199. // share email with other users
  200. function sendEmail(email) {
  201. room.sendCommand(Commands.EMAIL, {
  202. value: email,
  203. attributes: {
  204. id: room.myUserId()
  205. }
  206. });
  207. }
  208. var email = APP.settings.getEmail();
  209. email && sendEmail(email);
  210. APP.UI.addListener(UIEvents.EMAIL_CHANGED, function (email) {
  211. APP.settings.setEmail(email);
  212. APP.UI.setUserAvatar(room.myUserId(), email);
  213. sendEmail(email);
  214. });
  215. room.addCommandListener(Commands.EMAIL, function (data) {
  216. APP.UI.setUserAvatar(data.attributes.id, data.value);
  217. });
  218. room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, function (id, displayName) {
  219. APP.UI.changeDisplayName(id, displayName);
  220. });
  221. APP.UI.addListener(UIEvents.NICKNAME_CHANGED, function (nickname) {
  222. APP.settings.setDisplayName(nickname);
  223. room.setDisplayName(nickname);
  224. });
  225. APP.UI.addListener(UIEvents.MESSAGE_CREATED, function (message) {
  226. room.sendTextMessage(message);
  227. });
  228. room.on(ConferenceErrors.PASSWORD_REQUIRED, function () {
  229. // FIXME handle
  230. });
  231. room.on(ConferenceErrors.CONNECTION_ERROR, function () {
  232. // FIXME handle
  233. });
  234. APP.UI.addListener(
  235. UIEvents.START_MUTED_CHANGED,
  236. function (startAudioMuted, startVideoMuted) {
  237. // FIXME start muted
  238. }
  239. );
  240. return new Promise(function (resolve, reject) {
  241. room.on(
  242. ConferenceEvents.CONFERENCE_JOINED,
  243. function () {
  244. resolve();
  245. }
  246. );
  247. APP.UI.closeAuthenticationDialog();
  248. if (config.useNicks) {
  249. // FIXME check this
  250. var nick = APP.UI.askForNickname();
  251. }
  252. room.join();
  253. }).catch(function (err) {
  254. if (err[0] === ConferenceErrors.PASSWORD_REQUIRED) {
  255. // FIXME ask for password and try again
  256. return initConference(connection, roomName);
  257. }
  258. // FIXME else notify that we cannot conenct to the room
  259. throw new Error(err[0]);
  260. });
  261. }
  262. function init() {
  263. connect().then(function (connection) {
  264. return initConference(connection, APP.UI.generateRoomName());
  265. }).then(function () {
  266. APP.UI.start();
  267. APP.UI.initConference();
  268. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  269. APP.translation.setLanguage(language);
  270. APP.settings.setLanguage(language);
  271. });
  272. APP.desktopsharing.init();
  273. APP.statistics.start();
  274. APP.connectionquality.init();
  275. APP.keyboardshortcut.init();
  276. });
  277. }
  278. /**
  279. * If we have an HTTP endpoint for getting config.json configured we're going to
  280. * read it and override properties from config.js and interfaceConfig.js.
  281. * If there is no endpoint we'll just continue with initialization.
  282. * Keep in mind that if the endpoint has been configured and we fail to obtain
  283. * the config for any reason then the conference won't start and error message
  284. * will be displayed to the user.
  285. */
  286. function obtainConfigAndInit() {
  287. var roomName = APP.UI.getRoomNode();
  288. if (config.configLocation) {
  289. APP.configFetch.obtainConfig(
  290. config.configLocation, roomName,
  291. // Get config result callback
  292. function(success, error) {
  293. if (success) {
  294. console.log("(TIME) configuration fetched:\t",
  295. window.performance.now());
  296. init();
  297. } else {
  298. // Show obtain config error,
  299. // pass the error object for report
  300. APP.UI.messageHandler.openReportDialog(
  301. null, "dialog.connectError", error);
  302. }
  303. });
  304. } else {
  305. require("./modules/config/BoshAddressChoice").chooseAddress(
  306. config, roomName);
  307. init();
  308. }
  309. }
  310. $(document).ready(function () {
  311. console.log("(TIME) document ready:\t", window.performance.now());
  312. var URLProcessor = require("./modules/config/URLProcessor");
  313. URLProcessor.setConfigParametersFromUrl();
  314. APP.init();
  315. APP.translation.init();
  316. if (APP.API.isEnabled()) {
  317. APP.API.init();
  318. }
  319. obtainConfigAndInit();
  320. });
  321. $(window).bind('beforeunload', function () {
  322. if (APP.API.isEnabled()) {
  323. APP.API.dispose();
  324. }
  325. });
  326. module.exports = APP;