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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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._stropheConn.connected === true;
  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. * FIXME.
  60. *
  61. * @returns {*}
  62. */
  63. get emuc() {
  64. return this._stropheConn.emuc;
  65. }
  66. /**
  67. * Tells if Websocket is used as the transport for the current XMPP connection. Returns true for Websocket or false
  68. * for BOSH.
  69. * @returns {boolean}
  70. */
  71. get isUsingWebSocket() {
  72. return this._usesWebsocket;
  73. }
  74. /**
  75. * FIXME.
  76. *
  77. * @returns {string|null}
  78. */
  79. get jid() {
  80. return this._stropheConn.jid;
  81. }
  82. /**
  83. * FIXME.
  84. *
  85. * @returns {*}
  86. */
  87. get jingle() {
  88. return this._stropheConn.jingle;
  89. }
  90. /**
  91. * FIXME.
  92. *
  93. * @returns {string}
  94. */
  95. get lastResponseHeaders() {
  96. return this._stropheConn._proto && this._stropheConn._proto.lastResponseHeaders;
  97. }
  98. /**
  99. * FIXME.
  100. *
  101. * @returns {*}
  102. */
  103. get logger() {
  104. return this._stropheConn.logger;
  105. }
  106. /**
  107. * FIXME.
  108. *
  109. * @returns {*}
  110. */
  111. get options() {
  112. return this._stropheConn.options;
  113. }
  114. /**
  115. * FIXME.
  116. *
  117. * @returns {PingConnectionPlugin}
  118. */
  119. get ping() {
  120. return this._stropheConn.ping;
  121. }
  122. /**
  123. * FIXME.
  124. *
  125. * @returns {*}
  126. */
  127. get rayo() {
  128. return this._stropheConn.rayo;
  129. }
  130. /**
  131. * FIXME.
  132. *
  133. * @returns {string}
  134. */
  135. get service() {
  136. return this._stropheConn.service;
  137. }
  138. /**
  139. * FIXME.
  140. *
  141. * @param {number} _nextValidRid - FIXME.
  142. * @returns {void}
  143. */
  144. set nextValidRid(_nextValidRid) {
  145. // FIXME test
  146. this._stropheConn.nextValidRid = _nextValidRid;
  147. }
  148. /**
  149. * FIXME.
  150. *
  151. * @param {string} _service - FIXME.
  152. * @returns {void}
  153. */
  154. set service(_service) {
  155. this._stropheConn.service = _service;
  156. }
  157. /**
  158. * FIXME.
  159. *
  160. * @returns {void}
  161. */
  162. addHandler(...args) {
  163. this._stropheConn.addHandler(...args);
  164. }
  165. /**
  166. * FIXME.
  167. *
  168. * @returns {void}
  169. */
  170. attach(...args) {
  171. this._stropheConn.attach(...args);
  172. }
  173. /**
  174. * FIXME.
  175. *
  176. * @returns {void}
  177. */
  178. connect(...args) {
  179. this._stropheConn.connect(...args);
  180. }
  181. /**
  182. * FIXME.
  183. *
  184. * @returns {void}
  185. */
  186. closeWebsocket() {
  187. this._stropheConn._proto && this._stropheConn._proto.socket && this._stropheConn._proto.socket.close();
  188. }
  189. /**
  190. * FIXME.
  191. *
  192. * @returns {void}
  193. */
  194. disconnect(...args) {
  195. this._stropheConn.disconnect(...args);
  196. }
  197. /**
  198. * FIXME.
  199. *
  200. * @returns {void}
  201. */
  202. flush(...args) {
  203. this._stropheConn.flush(...args);
  204. }
  205. /**
  206. * See {@link LastRequestTracker.getTimeSinceLastSuccess}.
  207. *
  208. * @returns {number|null}
  209. */
  210. getTimeSinceLastBOSHSuccess() {
  211. return this._lastSuccessTracker
  212. ? this._lastSuccessTracker.getTimeSinceLastSuccess()
  213. : null;
  214. }
  215. /**
  216. * Send a stanza. This function is called to push data onto the send queue to go out over the wire.
  217. *
  218. * @param {Element|Strophe.Builder} stanza - The stanza to send.
  219. * @returns {void}
  220. */
  221. send(stanza) {
  222. if (!this.connected) {
  223. throw new Error('Not connected');
  224. }
  225. this._stropheConn.send(stanza);
  226. }
  227. /**
  228. * Helper function to send IQ stanzas.
  229. *
  230. * @param {Element} elem - The stanza to send.
  231. * @param {Function} callback - The callback function for a successful request.
  232. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  233. * be null.
  234. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  235. * @returns {number} - The id used to send the IQ.
  236. */
  237. sendIQ(elem, callback, errback, timeout) {
  238. if (!this.connected) {
  239. errback('Not connected');
  240. return;
  241. }
  242. return this._stropheConn.sendIQ(elem, callback, errback, timeout);
  243. }
  244. /**
  245. * Helper function to send presence stanzas. The main benefit is for sending presence stanzas for which you expect
  246. * a responding presence stanza with the same id (for example when leaving a chat room).
  247. *
  248. * @param {Element} elem - The stanza to send.
  249. * @param {Function} callback - The callback function for a successful request.
  250. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  251. * be null.
  252. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  253. * @returns {number} - The id used to send the presence.
  254. */
  255. sendPresence(elem, callback, errback, timeout) {
  256. if (!this.connected) {
  257. errback('Not connected');
  258. return;
  259. }
  260. this._stropheConn.sendPresence(elem, callback, errback, timeout);
  261. }
  262. /**
  263. * FIXME.
  264. *
  265. * @returns {void}
  266. */
  267. sendUnavailableBeacon() {
  268. this._stropheConn._changeConnectStatus(Strophe.Status.DISCONNECTING);
  269. this._stropheConn.disconnecting = true;
  270. const body = this._stropheConn._proto._buildBody()
  271. .attrs({
  272. type: 'terminate'
  273. });
  274. const pres = $pres({
  275. xmlns: Strophe.NS.CLIENT,
  276. type: 'unavailable'
  277. });
  278. body.cnode(pres.tree());
  279. const res = navigator.sendBeacon(
  280. `https:${this.service}`,
  281. Strophe.serialize(body.tree()));
  282. logger.info(`Successfully send unavailable beacon ${res}`);
  283. this._stropheConn._proto._abortAllRequests();
  284. this._stropheConn._doDisconnect();
  285. }
  286. }