Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

XmppConnection.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * 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. * FIXME.
  108. *
  109. * @param {number} _nextValidRid - FIXME.
  110. * @returns {void}
  111. */
  112. set nextValidRid(_nextValidRid) {
  113. // FIXME test
  114. this._stropheConn.nextValidRid = _nextValidRid;
  115. }
  116. /**
  117. * FIXME.
  118. *
  119. * @param {string} _service - FIXME.
  120. * @returns {void}
  121. */
  122. set service(_service) {
  123. this._stropheConn.service = _service;
  124. }
  125. /**
  126. * Adds a connection plugin to this instance.
  127. *
  128. * @param {string} name - The name of the plugin or rather a key under which it will be stored on this connection
  129. * instance.
  130. * @param {ConnectionPluginListenable} plugin - The plugin to add.
  131. */
  132. addConnectionPlugin(name, plugin) {
  133. this[name] = plugin;
  134. plugin.init(this);
  135. }
  136. /**
  137. * FIXME.
  138. *
  139. * @returns {void}
  140. */
  141. addHandler(...args) {
  142. this._stropheConn.addHandler(...args);
  143. }
  144. /**
  145. * FIXME.
  146. *
  147. * @returns {void}
  148. */
  149. attach(...args) {
  150. this._stropheConn.attach(...args);
  151. }
  152. /**
  153. * FIXME.
  154. *
  155. * @returns {void}
  156. */
  157. connect(...args) {
  158. this._stropheConn.connect(...args);
  159. }
  160. /**
  161. * FIXME.
  162. *
  163. * @returns {void}
  164. */
  165. closeWebsocket() {
  166. this._stropheConn._proto && this._stropheConn._proto.socket && this._stropheConn._proto.socket.close();
  167. }
  168. /**
  169. * FIXME.
  170. *
  171. * @returns {void}
  172. */
  173. disconnect(...args) {
  174. this._stropheConn.disconnect(...args);
  175. }
  176. /**
  177. * FIXME.
  178. *
  179. * @returns {void}
  180. */
  181. flush(...args) {
  182. this._stropheConn.flush(...args);
  183. }
  184. /**
  185. * See {@link LastRequestTracker.getTimeSinceLastSuccess}.
  186. *
  187. * @returns {number|null}
  188. */
  189. getTimeSinceLastBOSHSuccess() {
  190. return this._lastSuccessTracker
  191. ? this._lastSuccessTracker.getTimeSinceLastSuccess()
  192. : null;
  193. }
  194. /**
  195. * Send a stanza. This function is called to push data onto the send queue to go out over the wire.
  196. *
  197. * @param {Element|Strophe.Builder} stanza - The stanza to send.
  198. * @returns {void}
  199. */
  200. send(stanza) {
  201. if (!this.connected) {
  202. throw new Error('Not connected');
  203. }
  204. this._stropheConn.send(stanza);
  205. }
  206. /**
  207. * Helper function to send IQ stanzas.
  208. *
  209. * @param {Element} elem - The stanza to send.
  210. * @param {Function} callback - The callback function for a successful request.
  211. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  212. * be null.
  213. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  214. * @returns {number} - The id used to send the IQ.
  215. */
  216. sendIQ(elem, callback, errback, timeout) {
  217. if (!this.connected) {
  218. errback('Not connected');
  219. return;
  220. }
  221. return this._stropheConn.sendIQ(elem, callback, errback, timeout);
  222. }
  223. /**
  224. * Helper function to send presence stanzas. The main benefit is for sending presence stanzas for which you expect
  225. * a responding presence stanza with the same id (for example when leaving a chat room).
  226. *
  227. * @param {Element} elem - The stanza to send.
  228. * @param {Function} callback - The callback function for a successful request.
  229. * @param {Function} errback - The callback function for a failed or timed out request. On timeout, the stanza will
  230. * be null.
  231. * @param {number} timeout - The time specified in milliseconds for a timeout to occur.
  232. * @returns {number} - The id used to send the presence.
  233. */
  234. sendPresence(elem, callback, errback, timeout) {
  235. if (!this.connected) {
  236. errback('Not connected');
  237. return;
  238. }
  239. this._stropheConn.sendPresence(elem, callback, errback, timeout);
  240. }
  241. /**
  242. * FIXME.
  243. *
  244. * @returns {void}
  245. */
  246. sendUnavailableBeacon() {
  247. this._stropheConn._changeConnectStatus(Strophe.Status.DISCONNECTING);
  248. this._stropheConn.disconnecting = true;
  249. const body = this._stropheConn._proto._buildBody()
  250. .attrs({
  251. type: 'terminate'
  252. });
  253. const pres = $pres({
  254. xmlns: Strophe.NS.CLIENT,
  255. type: 'unavailable'
  256. });
  257. body.cnode(pres.tree());
  258. const res = navigator.sendBeacon(
  259. `https:${this.service}`,
  260. Strophe.serialize(body.tree()));
  261. logger.info(`Successfully send unavailable beacon ${res}`);
  262. this._stropheConn._proto._abortAllRequests();
  263. this._stropheConn._doDisconnect();
  264. }
  265. }