You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmpp.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* global $, APP, config, Strophe*/
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var EventEmitter = require("events");
  4. var Pako = require("pako");
  5. var RTCEvents = require("../../service/RTC/RTCEvents");
  6. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  7. var JitsiConnectionErrors = require("../../JitsiConnectionErrors");
  8. var JitsiConnectionEvents = require("../../JitsiConnectionEvents");
  9. var RTC = require("../RTC/RTC");
  10. var authenticatedUser = false;
  11. function createConnection(bosh) {
  12. bosh = bosh || '/http-bind';
  13. // Append token as URL param
  14. if (this.token) {
  15. bosh += bosh.indexOf('?') == -1 ?
  16. '?token=' + this.token : '&token=' + this.token;
  17. }
  18. return new Strophe.Connection(bosh);
  19. };
  20. //!!!!!!!!!! FIXME: ...
  21. function initStrophePlugins(XMPP)
  22. {
  23. require("./strophe.emuc")(XMPP);
  24. require("./strophe.jingle")(XMPP, XMPP.eventEmitter);
  25. // require("./strophe.moderate")(XMPP, eventEmitter);
  26. require("./strophe.util")();
  27. require("./strophe.ping")(XMPP, XMPP.eventEmitter);
  28. require("./strophe.rayo")();
  29. require("./strophe.logger")();
  30. }
  31. //!!!!!!!!!! FIXME: ...
  32. ///**
  33. // * If given <tt>localStream</tt> is video one this method will advertise it's
  34. // * video type in MUC presence.
  35. // * @param localStream new or modified <tt>LocalStream</tt>.
  36. // */
  37. //function broadcastLocalVideoType(localStream) {
  38. // if (localStream.videoType)
  39. // XMPP.addToPresence('videoType', localStream.videoType);
  40. //}
  41. //
  42. //function registerListeners() {
  43. // RTC.addStreamListener(
  44. // broadcastLocalVideoType,
  45. // StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  46. // );
  47. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  48. // XMPP.addToPresence("devices", devices);
  49. // });
  50. //}
  51. function XMPP(options) {
  52. this.eventEmitter = new EventEmitter();
  53. this.connection = null;
  54. this.disconnectInProgress = false;
  55. this.forceMuted = false;
  56. this.options = options;
  57. initStrophePlugins(this);
  58. // registerListeners();
  59. this.connection = createConnection(options.bosh);
  60. }
  61. XMPP.prototype.getConnection = function(){ return connection; };
  62. XMPP.prototype._connect = function (jid, password) {
  63. var self = this;
  64. // connection.connect() starts the connection process.
  65. //
  66. // As the connection process proceeds, the user supplied callback will
  67. // be triggered multiple times with status updates. The callback should
  68. // take two arguments - the status code and the error condition.
  69. //
  70. // The status code will be one of the values in the Strophe.Status
  71. // constants. The error condition will be one of the conditions defined
  72. // in RFC 3920 or the condition ‘strophe-parsererror’.
  73. //
  74. // The Parameters wait, hold and route are optional and only relevant
  75. // for BOSH connections. Please see XEP 124 for a more detailed
  76. // explanation of the optional parameters.
  77. //
  78. // Connection status constants for use by the connection handler
  79. // callback.
  80. //
  81. // Status.ERROR - An error has occurred (websockets specific)
  82. // Status.CONNECTING - The connection is currently being made
  83. // Status.CONNFAIL - The connection attempt failed
  84. // Status.AUTHENTICATING - The connection is authenticating
  85. // Status.AUTHFAIL - The authentication attempt failed
  86. // Status.CONNECTED - The connection has succeeded
  87. // Status.DISCONNECTED - The connection has been terminated
  88. // Status.DISCONNECTING - The connection is currently being terminated
  89. // Status.ATTACHED - The connection has been attached
  90. var anonymousConnectionFailed = false;
  91. var connectionFailed = false;
  92. var lastErrorMsg;
  93. this.connection.connect(jid, password, function (status, msg) {
  94. logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
  95. (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
  96. if (status === Strophe.Status.CONNECTED) {
  97. if (self.options.useStunTurn) {
  98. self.connection.jingle.getStunAndTurnCredentials();
  99. }
  100. logger.info("My Jabber ID: " + self.connection.jid);
  101. // Schedule ping ?
  102. var pingJid = self.connection.domain;
  103. self.connection.ping.hasPingSupport(
  104. pingJid,
  105. function (hasPing) {
  106. if (hasPing)
  107. self.connection.ping.startInterval(pingJid);
  108. else
  109. logger.warn("Ping NOT supported by " + pingJid);
  110. }
  111. );
  112. if (password)
  113. authenticatedUser = true;
  114. if (self.connection && self.connection.connected &&
  115. Strophe.getResourceFromJid(self.connection.jid)) {
  116. // .connected is true while connecting?
  117. // self.connection.send($pres());
  118. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  119. Strophe.getResourceFromJid(self.connection.jid));
  120. }
  121. } else if (status === Strophe.Status.CONNFAIL) {
  122. if (msg === 'x-strophe-bad-non-anon-jid') {
  123. anonymousConnectionFailed = true;
  124. }
  125. else
  126. {
  127. connectionFailed = true;
  128. }
  129. lastErrorMsg = msg;
  130. } else if (status === Strophe.Status.DISCONNECTED) {
  131. // Stop ping interval
  132. self.connection.ping.stopInterval();
  133. self.disconnectInProgress = false;
  134. if (anonymousConnectionFailed) {
  135. // prompt user for username and password
  136. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  137. JitsiConnectionErrors.PASSWORD_REQUIRED);
  138. } else if(connectionFailed) {
  139. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  140. JitsiConnectionErrors.OTHER_ERROR,
  141. msg ? msg : lastErrorMsg);
  142. } else {
  143. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  144. msg ? msg : lastErrorMsg);
  145. }
  146. } else if (status === Strophe.Status.AUTHFAIL) {
  147. // wrong password or username, prompt user
  148. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  149. JitsiConnectionErrors.PASSWORD_REQUIRED);
  150. }
  151. });
  152. }
  153. XMPP.prototype.connect = function (jid, password) {
  154. if(!jid) {
  155. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  156. // Force authenticated domain if room is appended with '?login=true'
  157. if (this.options.hosts.anonymousdomain &&
  158. window.location.search.indexOf("login=true") !== -1) {
  159. configDomain = this.options.hosts.domain;
  160. }
  161. jid = configDomain || window.location.hostname;
  162. }
  163. return this._connect(jid, password);
  164. };
  165. XMPP.prototype.createRoom = function (roomName, options) {
  166. var roomjid = roomName + '@' + this.options.hosts.muc;
  167. if (options.useNicks) {
  168. if (options.nick) {
  169. roomjid += '/' + options.nick;
  170. } else {
  171. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  172. }
  173. } else {
  174. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  175. if(!authenticatedUser)
  176. tmpJid = tmpJid.substr(0, 8);
  177. roomjid += '/' + tmpJid;
  178. }
  179. return this.connection.emuc.createRoom(roomjid, null, options);
  180. }
  181. XMPP.prototype.addListener = function(type, listener) {
  182. this.eventEmitter.on(type, listener);
  183. };
  184. XMPP.prototype.removeListener = function (type, listener) {
  185. this.eventEmitter.removeListener(type, listener);
  186. };
  187. //FIXME: this should work with the room
  188. XMPP.prototype.leaveRoom = function (jid) {
  189. var handler = this.connection.jingle.jid2session[jid];
  190. if (handler && handler.peerconnection) {
  191. // FIXME: probably removing streams is not required and close() should
  192. // be enough
  193. if (RTC.localAudio) {
  194. handler.peerconnection.removeStream(
  195. RTC.localAudio.getOriginalStream(), true);
  196. }
  197. if (RTC.localVideo) {
  198. handler.peerconnection.removeStream(
  199. RTC.localVideo.getOriginalStream(), true);
  200. }
  201. handler.peerconnection.close();
  202. }
  203. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
  204. this.connection.emuc.doLeave(jid);
  205. };
  206. /**
  207. * Sends 'data' as a log message to the focus. Returns true iff a message
  208. * was sent.
  209. * @param data
  210. * @returns {boolean} true iff a message was sent.
  211. */
  212. XMPP.prototype.sendLogs = function (data) {
  213. if(!this.connection.emuc.focusMucJid)
  214. return false;
  215. var deflate = true;
  216. var content = JSON.stringify(data);
  217. if (deflate) {
  218. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  219. }
  220. content = Base64.encode(content);
  221. // XEP-0337-ish
  222. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  223. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  224. id: 'PeerConnectionStats'});
  225. message.c('message').t(content).up();
  226. if (deflate) {
  227. message.c('tag', {name: "deflated", value: "true"}).up();
  228. }
  229. message.up();
  230. this.connection.send(message);
  231. return true;
  232. };
  233. // Gets the logs from strophe.jingle.
  234. XMPP.prototype.getJingleLog = function () {
  235. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  236. };
  237. // Gets the logs from strophe.
  238. XMPP.prototype.getXmppLog = function () {
  239. return this.connection.logger ? this.connection.logger.log : null;
  240. };
  241. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  242. this.connection.rayo.dial(to, from, roomName,roomPass);
  243. };
  244. XMPP.prototype.setMute = function (jid, mute) {
  245. this.connection.moderate.setMute(jid, mute);
  246. };
  247. XMPP.prototype.eject = function (jid) {
  248. this.connection.moderate.eject(jid);
  249. };
  250. XMPP.prototype.getSessions = function () {
  251. return this.connection.jingle.sessions;
  252. };
  253. XMPP.prototype.disconnect = function () {
  254. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  255. {
  256. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  257. return;
  258. }
  259. this.disconnectInProgress = true;
  260. this.connection.disconnect();
  261. };
  262. /**
  263. * Gets the SSRC of local media stream.
  264. * @param mediaType the media type that tells whether we want to get
  265. * the SSRC of local audio or video stream.
  266. * @returns {*} the SSRC number for local media stream or <tt>null</tt> if
  267. * not available.
  268. */
  269. XMPP.prototype.getLocalSSRC = function (mediaType) {
  270. if (this.connection.jingle.activecall &&
  271. this.connection.jingle.activecall.peerconnection) {
  272. return this.connection.jingle.activecall.getLocalSSRC(mediaType);
  273. } else {
  274. return null;
  275. }
  276. };
  277. module.exports = XMPP;