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.

XmppConnection.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import { $pres, Strophe } from 'strophe.js';
  3. import LastSuccessTracker from './StropheBoshLastSuccess';
  4. const logger = getLogger(__filename);
  5. /**
  6. * FIXME.
  7. */
  8. export default class XmppConnection {
  9. /**
  10. * FIXME.
  11. *
  12. * @param {XMPP} xmpp - FIXME.
  13. * @param {String} serviceUrl - FIXME.
  14. */
  15. constructor(xmpp, serviceUrl) {
  16. this.xmpp = xmpp;
  17. this._stropheConn = new Strophe.Connection(serviceUrl);
  18. this._usesWebsocket = serviceUrl.startsWith('ws:') || serviceUrl.startsWith('wss:');
  19. // The default maxRetries is 5, which is too long.
  20. this._stropheConn.maxRetries = 3;
  21. if (!this._usesWebsocket) {
  22. this._lastSuccessTracker = new LastSuccessTracker();
  23. this._lastSuccessTracker.startTracking(this._stropheConn);
  24. }
  25. }
  26. /**
  27. * FIXME.
  28. *
  29. * @returns {boolean}
  30. */
  31. get connected() {
  32. return this._status === Strophe.Status.CONNECTED;
  33. }
  34. /**
  35. * FIXME.
  36. *
  37. * @returns {Strophe.Connection.disco}
  38. */
  39. get disco() {
  40. return this._stropheConn.disco;
  41. }
  42. /**
  43. * FIXME.
  44. *
  45. * @returns {boolean}
  46. */
  47. get disconnecting() {
  48. return this._stropheConn.disconnecting === true;
  49. }
  50. /**
  51. * FIXME.
  52. *
  53. * @returns {string|null}
  54. */
  55. get domain() {
  56. return this._stropheConn.domain;
  57. }
  58. /**
  59. * Tells if Websocket is used as the transport for the current XMPP connection. Returns true for Websocket or false
  60. * for BOSH.
  61. * @returns {boolean}
  62. */
  63. get isUsingWebSocket() {
  64. return this._usesWebsocket;
  65. }
  66. /**
  67. * FIXME.
  68. *
  69. * @returns {string|null}
  70. */
  71. get jid() {
  72. return this._stropheConn.jid;
  73. }
  74. /**
  75. * FIXME.
  76. *
  77. * @returns {string}
  78. */
  79. get lastResponseHeaders() {
  80. return this._stropheConn._proto && this._stropheConn._proto.lastResponseHeaders;
  81. }
  82. /**
  83. * FIXME.
  84. *
  85. * @returns {*}
  86. */
  87. get logger() {
  88. return this._stropheConn.logger;
  89. }
  90. /**
  91. * FIXME.
  92. *
  93. * @returns {*}
  94. */
  95. get options() {
  96. return this._stropheConn.options;
  97. }
  98. /**
  99. * FIXME.
  100. *
  101. * @returns {string}
  102. */
  103. get service() {
  104. return this._stropheConn.service;
  105. }
  106. /**
  107. * Returns the current connection status.
  108. *
  109. * @returns {Strophe.Status}
  110. */
  111. get status() {
  112. return this._status;
  113. }
  114. /**
  115. * FIXME.
  116. *
  117. * @param {number} _nextValidRid - FIXME.
  118. * @returns {void}
  119. */
  120. set nextValidRid(_nextValidRid) {
  121. // FIXME test
  122. this._stropheConn.nextValidRid = _nextValidRid;
  123. }
  124. /**
  125. * FIXME.
  126. *
  127. * @param {string} _service - FIXME.
  128. * @returns {void}
  129. */
  130. set service(_service) {
  131. this._stropheConn.service = _service;
  132. }
  133. /**
  134. * Adds a connection plugin to this instance.
  135. *
  136. * @param {string} name - The name of the plugin or rather a key under which it will be stored on this connection
  137. * instance.
  138. * @param {ConnectionPluginListenable} plugin - The plugin to add.
  139. */
  140. addConnectionPlugin(name, plugin) {
  141. this[name] = plugin;
  142. plugin.init(this);
  143. }
  144. /**
  145. * FIXME.
  146. *
  147. * @returns {void}
  148. */
  149. addHandler(...args) {
  150. this._stropheConn.addHandler(...args);
  151. }
  152. /**
  153. * FIXME.
  154. *
  155. * @returns {void}
  156. */
  157. attach(...args) {
  158. this._stropheConn.attach(...args);
  159. }
  160. /**
  161. * Wraps Strophe.Connection.connect method in order to intercept the connection status updates.
  162. * See {@link Strophe.Connection.connect} for the params description.
  163. *
  164. * @returns {void}
  165. */
  166. connect(jid, pass, callback, ...args) {
  167. const connectCb = (status, condition) => {
  168. this._status = status;
  169. callback(status, condition);
  170. };
  171. this._stropheConn.connect(jid, pass, connectCb, ...args);
  172. }
  173. /**
  174. * FIXME.
  175. *
  176. * @returns {void}
  177. */
  178. closeWebsocket() {
  179. this._stropheConn._proto && this._stropheConn._proto.socket && this._stropheConn._proto.socket.close();
  180. }
  181. /**
  182. * FIXME.
  183. *
  184. * @returns {void}
  185. */
  186. disconnect(...args) {
  187. this._stropheConn.disconnect(...args);
  188. }
  189. /**
  190. * FIXME.
  191. *
  192. * @returns {void}
  193. */
  194. flush(...args) {
  195. this._stropheConn.flush(...args);
  196. }
  197. /**
  198. * See {@link LastRequestTracker.getTimeSinceLastSuccess}.
  199. *
  200. * @returns {number|null}
  201. */
  202. getTimeSinceLastBOSHSuccess() {
  203. return this._lastSuccessTracker
  204. ? this._lastSuccessTracker.getTimeSinceLastSuccess()
  205. : null;
  206. }
  207. /**
  208. * Send a stanza. This function is called to push data onto the send queue to go out over the wire.
  209. *
  210. * @param {Element|Strophe.Builder} stanza - The stanza to send.
  211. * @returns {void}
  212. */
  213. send(stanza) {
  214. if (!this.connected) {
  215. throw new Error('Not connected');
  216. }
  217. this._stropheConn.send(stanza);
  218. }
  219. /**
  220. * Helper function to send IQ stanzas.
  221. *
  222. * @param {Element} elem - The stanza to send.
  223. * @param {Function} callback - The callback function for a successful request.
  224. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  225. * be null.
  226. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  227. * @returns {number} - The id used to send the IQ.
  228. */
  229. sendIQ(elem, callback, errback, timeout) {
  230. if (!this.connected) {
  231. errback('Not connected');
  232. return;
  233. }
  234. return this._stropheConn.sendIQ(elem, callback, errback, timeout);
  235. }
  236. /**
  237. * Helper function to send presence stanzas. The main benefit is for sending presence stanzas for which you expect
  238. * a responding presence stanza with the same id (for example when leaving a chat room).
  239. *
  240. * @param {Element} elem - The stanza to send.
  241. * @param {Function} callback - The callback function for a successful request.
  242. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  243. * be null.
  244. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  245. * @returns {number} - The id used to send the presence.
  246. */
  247. sendPresence(elem, callback, errback, timeout) {
  248. if (!this.connected) {
  249. errback('Not connected');
  250. return;
  251. }
  252. this._stropheConn.sendPresence(elem, callback, errback, timeout);
  253. }
  254. /**
  255. * FIXME.
  256. *
  257. * @returns {void}
  258. */
  259. sendUnavailableBeacon() {
  260. this._stropheConn._changeConnectStatus(Strophe.Status.DISCONNECTING);
  261. this._stropheConn.disconnecting = true;
  262. const body = this._stropheConn._proto._buildBody()
  263. .attrs({
  264. type: 'terminate'
  265. });
  266. const pres = $pres({
  267. xmlns: Strophe.NS.CLIENT,
  268. type: 'unavailable'
  269. });
  270. body.cnode(pres.tree());
  271. const res = navigator.sendBeacon(
  272. `https:${this.service}`,
  273. Strophe.serialize(body.tree()));
  274. logger.info(`Successfully send unavailable beacon ${res}`);
  275. this._stropheConn._proto._abortAllRequests();
  276. this._stropheConn._doDisconnect();
  277. }
  278. }