Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

xmpp.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* global $, APP, config, Strophe*/
  2. var Moderator = require("./moderator");
  3. var EventEmitter = require("events");
  4. var Pako = require("pako");
  5. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  6. var RTCEvents = require("../../service/RTC/RTCEvents");
  7. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  8. var JitsiConnectionErrors = require("../../JitsiConnectionErrors");
  9. var JitsiConnectionEvents = require("../../JitsiConnectionEvents");
  10. var RTC = require("../RTC/RTC");
  11. var authenticatedUser = false;
  12. function createConnection(bosh) {
  13. bosh = bosh || '/http-bind';
  14. return new Strophe.Connection(bosh);
  15. };
  16. //!!!!!!!!!! FIXME: ...
  17. function initStrophePlugins(XMPP)
  18. {
  19. require("./strophe.emuc")(XMPP);
  20. // require("./strophe.jingle")(XMPP, eventEmitter);
  21. // require("./strophe.moderate")(XMPP, eventEmitter);
  22. require("./strophe.util")();
  23. require("./strophe.rayo")();
  24. require("./strophe.logger")();
  25. }
  26. //!!!!!!!!!! FIXME: ...
  27. ///**
  28. // * If given <tt>localStream</tt> is video one this method will advertise it's
  29. // * video type in MUC presence.
  30. // * @param localStream new or modified <tt>LocalStream</tt>.
  31. // */
  32. //function broadcastLocalVideoType(localStream) {
  33. // if (localStream.videoType)
  34. // XMPP.addToPresence('videoType', localStream.videoType);
  35. //}
  36. //
  37. //function registerListeners() {
  38. // RTC.addStreamListener(
  39. // broadcastLocalVideoType,
  40. // StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  41. // );
  42. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  43. // XMPP.addToPresence("devices", devices);
  44. // });
  45. //}
  46. function XMPP(options) {
  47. this.sessionTerminated = false;
  48. this.eventEmitter = new EventEmitter();
  49. this.connection = null;
  50. this.disconnectInProgress = false;
  51. this.forceMuted = false;
  52. this.options = options;
  53. initStrophePlugins(this);
  54. // registerListeners();
  55. Moderator.init(this, this.eventEmitter);
  56. this.connection = createConnection(options.bosh);
  57. Moderator.setConnection(this.connection);
  58. }
  59. XMPP.prototype.getConnection = function(){ return connection; };
  60. XMPP.prototype._connect = function (jid, password) {
  61. var self = this;
  62. // connection.connect() starts the connection process.
  63. //
  64. // As the connection process proceeds, the user supplied callback will
  65. // be triggered multiple times with status updates. The callback should
  66. // take two arguments - the status code and the error condition.
  67. //
  68. // The status code will be one of the values in the Strophe.Status
  69. // constants. The error condition will be one of the conditions defined
  70. // in RFC 3920 or the condition ‘strophe-parsererror’.
  71. //
  72. // The Parameters wait, hold and route are optional and only relevant
  73. // for BOSH connections. Please see XEP 124 for a more detailed
  74. // explanation of the optional parameters.
  75. //
  76. // Connection status constants for use by the connection handler
  77. // callback.
  78. //
  79. // Status.ERROR - An error has occurred (websockets specific)
  80. // Status.CONNECTING - The connection is currently being made
  81. // Status.CONNFAIL - The connection attempt failed
  82. // Status.AUTHENTICATING - The connection is authenticating
  83. // Status.AUTHFAIL - The authentication attempt failed
  84. // Status.CONNECTED - The connection has succeeded
  85. // Status.DISCONNECTED - The connection has been terminated
  86. // Status.DISCONNECTING - The connection is currently being terminated
  87. // Status.ATTACHED - The connection has been attached
  88. var anonymousConnectionFailed = false;
  89. var connectionFailed = false;
  90. var lastErrorMsg;
  91. this.connection.connect(jid, password, function (status, msg) {
  92. console.log('Strophe status changed to',
  93. Strophe.getStatusString(status), msg);
  94. if (status === Strophe.Status.CONNECTED) {
  95. if (self.options.useStunTurn) {
  96. self.connection.jingle.getStunAndTurnCredentials();
  97. }
  98. console.info("My Jabber ID: " + self.connection.jid);
  99. if (password)
  100. authenticatedUser = true;
  101. if (self.connection && self.connection.connected &&
  102. Strophe.getResourceFromJid(self.connection.jid)) {
  103. // .connected is true while connecting?
  104. self.connection.send($pres());
  105. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  106. Strophe.getResourceFromJid(self.connection.jid));
  107. }
  108. } else if (status === Strophe.Status.CONNFAIL) {
  109. if (msg === 'x-strophe-bad-non-anon-jid') {
  110. anonymousConnectionFailed = true;
  111. }
  112. else
  113. {
  114. connectionFailed = true;
  115. }
  116. lastErrorMsg = msg;
  117. } else if (status === Strophe.Status.DISCONNECTED) {
  118. self.disconnectInProgress = false;
  119. if (anonymousConnectionFailed) {
  120. // prompt user for username and password
  121. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  122. JitsiConnectionErrors.PASSWORD_REQUIRED);
  123. } else if(connectionFailed) {
  124. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  125. JitsiConnectionErrors.OTHER_ERROR,
  126. msg ? msg : lastErrorMsg);
  127. } else {
  128. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  129. msg ? msg : lastErrorMsg);
  130. }
  131. } else if (status === Strophe.Status.AUTHFAIL) {
  132. // wrong password or username, prompt user
  133. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  134. JitsiConnectionErrors.PASSWORD_REQUIRED);
  135. }
  136. });
  137. }
  138. XMPP.prototype.connect = function (jid, password) {
  139. if(!jid) {
  140. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  141. // Force authenticated domain if room is appended with '?login=true'
  142. if (this.options.hosts.anonymousdomain &&
  143. window.location.search.indexOf("login=true") !== -1) {
  144. configDomain = this.options.hosts.domain;
  145. }
  146. jid = configDomain || window.location.hostname;
  147. }
  148. return this._connect(jid, password);
  149. };
  150. XMPP.prototype.joinRoom = function(roomName, useNicks, nick) {
  151. var roomjid = roomName + '@' + Strophe.getDomainFromJid(this.connection.jid);
  152. if (useNicks) {
  153. if (nick) {
  154. roomjid += '/' + nick;
  155. } else {
  156. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  157. }
  158. } else {
  159. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  160. if(!authenticatedUser)
  161. tmpJid = tmpJid.substr(0, 8);
  162. roomjid += '/' + tmpJid;
  163. }
  164. return this.connection.emuc.doJoin(roomjid, null, this.eventEmitter);
  165. };
  166. XMPP.prototype.disposeConference = function (onUnload) {
  167. var handler = this.connection.jingle.activecall;
  168. if (handler && handler.peerconnection) {
  169. // FIXME: probably removing streams is not required and close() should
  170. // be enough
  171. if (RTC.localAudio) {
  172. handler.peerconnection.removeStream(
  173. RTC.localAudio.getOriginalStream(), onUnload);
  174. }
  175. if (RTC.localVideo) {
  176. handler.peerconnection.removeStream(
  177. RTC.localVideo.getOriginalStream(), onUnload);
  178. }
  179. handler.peerconnection.close();
  180. }
  181. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE, onUnload);
  182. this.connection.jingle.activecall = null;
  183. if (!onUnload) {
  184. this.sessionTerminated = true;
  185. this.connection.emuc.doLeave();
  186. }
  187. };
  188. XMPP.prototype.addListener = function(type, listener) {
  189. this.eventEmitter.on(type, listener);
  190. };
  191. XMPP.prototype.removeListener = function (type, listener) {
  192. this.eventEmitter.removeListener(type, listener);
  193. };
  194. XMPP.prototype.leaveRoom = function (jid) {
  195. this.connection.emuc.doLeave(jid);
  196. };
  197. XMPP.prototype.allocateConferenceFocus = function(roomName, callback) {
  198. Moderator.allocateConferenceFocus(roomName, callback);
  199. };
  200. XMPP.prototype.getLoginUrl = function (roomName, callback) {
  201. Moderator.getLoginUrl(roomName, callback);
  202. }
  203. XMPP.prototype.getPopupLoginUrl = function (roomName, callback) {
  204. Moderator.getPopupLoginUrl(roomName, callback);
  205. };
  206. XMPP.prototype.isModerator = function () {
  207. return Moderator.isModerator();
  208. };
  209. XMPP.prototype.isSipGatewayEnabled = function () {
  210. return Moderator.isSipGatewayEnabled();
  211. }
  212. XMPP.prototype.isExternalAuthEnabled = function () {
  213. return Moderator.isExternalAuthEnabled();
  214. };
  215. XMPP.prototype.isConferenceInProgress = function () {
  216. return this.connection && this.connection.jingle.activecall &&
  217. this.connection.jingle.activecall.peerconnection;
  218. };
  219. XMPP.prototype.switchStreams = function (stream, oldStream, callback, isAudio) {
  220. if (this.isConferenceInProgress()) {
  221. // FIXME: will block switchInProgress on true value in case of exception
  222. this.connection.jingle.activecall.switchStreams(stream, oldStream, callback, isAudio);
  223. } else {
  224. // We are done immediately
  225. console.warn("No conference handler or conference not started yet");
  226. callback();
  227. }
  228. };
  229. XMPP.prototype.sendVideoInfoPresence = function (mute) {
  230. if(!this.connection)
  231. return;
  232. this.connection.emuc.addVideoInfoToPresence(mute);
  233. this.connection.emuc.sendPresence();
  234. };
  235. XMPP.prototype.setVideoMute = function (mute, callback, options) {
  236. if(!this.connection)
  237. return;
  238. var self = this;
  239. var localCallback = function (mute) {
  240. self.sendVideoInfoPresence(mute);
  241. return callback(mute);
  242. };
  243. if(this.connection.jingle.activecall)
  244. {
  245. this.connection.jingle.activecall.setVideoMute(
  246. mute, localCallback, options);
  247. }
  248. else {
  249. localCallback(mute);
  250. }
  251. };
  252. XMPP.prototype.setAudioMute = function (mute, callback) {
  253. if (!(this.connection && RTC.localAudio)) {
  254. return false;
  255. }
  256. if (this.forceMuted && !mute) {
  257. console.info("Asking focus for unmute");
  258. this.connection.moderate.setMute(this.connection.emuc.myroomjid, mute);
  259. // FIXME: wait for result before resetting muted status
  260. this.forceMuted = false;
  261. }
  262. if (mute == RTC.localAudio.isMuted()) {
  263. // Nothing to do
  264. return true;
  265. }
  266. RTC.localAudio.setMute(mute);
  267. this.sendAudioInfoPresence(mute, callback);
  268. return true;
  269. };
  270. XMPP.prototype.sendAudioInfoPresence = function(mute, callback) {
  271. if(this.connection) {
  272. this.connection.emuc.addAudioInfoToPresence(mute);
  273. this.connection.emuc.sendPresence();
  274. }
  275. callback();
  276. return true;
  277. };
  278. /**
  279. * Sends 'data' as a log message to the focus. Returns true iff a message
  280. * was sent.
  281. * @param data
  282. * @returns {boolean} true iff a message was sent.
  283. */
  284. XMPP.prototype.sendLogs = function (data) {
  285. if(!this.connection.emuc.focusMucJid)
  286. return false;
  287. var deflate = true;
  288. var content = JSON.stringify(data);
  289. if (deflate) {
  290. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  291. }
  292. content = Base64.encode(content);
  293. // XEP-0337-ish
  294. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  295. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  296. id: 'PeerConnectionStats'});
  297. message.c('message').t(content).up();
  298. if (deflate) {
  299. message.c('tag', {name: "deflated", value: "true"}).up();
  300. }
  301. message.up();
  302. this.connection.send(message);
  303. return true;
  304. };
  305. // Gets the logs from strophe.jingle.
  306. XMPP.prototype.getJingleLog = function () {
  307. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  308. };
  309. // Gets the logs from strophe.
  310. XMPP.prototype.getXmppLog = function () {
  311. return this.connection.logger ? this.connection.logger.log : null;
  312. };
  313. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  314. this.connection.rayo.dial(to, from, roomName,roomPass);
  315. };
  316. XMPP.prototype.setMute = function (jid, mute) {
  317. this.connection.moderate.setMute(jid, mute);
  318. };
  319. XMPP.prototype.eject = function (jid) {
  320. this.connection.moderate.eject(jid);
  321. };
  322. XMPP.prototype.logout = function (callback) {
  323. Moderator.logout(callback);
  324. };
  325. XMPP.prototype.getJidFromSSRC = function (ssrc) {
  326. if (!this.isConferenceInProgress())
  327. return null;
  328. return this.connection.jingle.activecall.getSsrcOwner(ssrc);
  329. };
  330. XMPP.prototype.getSessions = function () {
  331. return this.connection.jingle.sessions;
  332. };
  333. XMPP.prototype.removeStream = function (stream) {
  334. if (!this.isConferenceInProgress())
  335. return;
  336. this.connection.jingle.activecall.peerconnection.removeStream(stream);
  337. };
  338. XMPP.prototype.disconnect = function (callback) {
  339. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  340. {
  341. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  342. return;
  343. }
  344. this.disconnectInProgress = true;
  345. this.connection.disconnect();
  346. };
  347. module.exports = XMPP;