|
@@ -92,7 +92,11 @@ export default class XMPP extends Listenable {
|
92
|
92
|
// they wanted to utilize the connected connection in an unload handler
|
93
|
93
|
// of their own. However, it should be fairly easy for them to do that
|
94
|
94
|
// by registering their unload handler before us.
|
95
|
|
- $(window).on('beforeunload unload', this.disconnect.bind(this));
|
|
95
|
+ $(window).on('beforeunload unload', ev => {
|
|
96
|
+ this.disconnect(ev).catch(() => {
|
|
97
|
+ // ignore errors in order to not brake the unload.
|
|
98
|
+ });
|
|
99
|
+ });
|
96
|
100
|
}
|
97
|
101
|
|
98
|
102
|
/**
|
|
@@ -171,6 +175,8 @@ export default class XMPP extends Listenable {
|
171
|
175
|
logger.log(
|
172
|
176
|
`(TIME) Strophe ${statusStr}${msg ? `[${msg}]` : ''}:\t`,
|
173
|
177
|
now);
|
|
178
|
+
|
|
179
|
+ this.eventEmitter.emit(XMPPEvents.CONNECTION_STATUS_CHANGED, credentials, status, msg);
|
174
|
180
|
if (status === Strophe.Status.CONNECTED
|
175
|
181
|
|| status === Strophe.Status.ATTACHED) {
|
176
|
182
|
if (this.options.useStunTurn
|
|
@@ -490,50 +496,62 @@ export default class XMPP extends Listenable {
|
490
|
496
|
/**
|
491
|
497
|
* Disconnects this from the XMPP server (if this is connected).
|
492
|
498
|
*
|
493
|
|
- * @param ev optionally, the event which triggered the necessity to
|
|
499
|
+ * @param {Object} ev - Optionally, the event which triggered the necessity to
|
494
|
500
|
* disconnect from the XMPP server (e.g. beforeunload, unload).
|
|
501
|
+ * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
|
495
|
502
|
*/
|
496
|
503
|
disconnect(ev) {
|
497
|
504
|
if (this.disconnectInProgress || !this.connection) {
|
498
|
505
|
this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
|
499
|
506
|
|
500
|
|
- return;
|
|
507
|
+ return Promise.reject(new Error('Wrong connection state!'));
|
501
|
508
|
}
|
502
|
509
|
|
503
|
510
|
this.disconnectInProgress = true;
|
504
|
511
|
|
505
|
|
- // XXX Strophe is asynchronously sending by default. Unfortunately, that
|
506
|
|
- // means that there may not be enough time to send an unavailable
|
507
|
|
- // presence or disconnect at all. Switching Strophe to synchronous
|
508
|
|
- // sending is not much of an option because it may lead to a noticeable
|
509
|
|
- // delay in navigating away from the current location. As a compromise,
|
510
|
|
- // we will try to increase the chances of sending an unavailable
|
511
|
|
- // presence and/or disconecting within the short time span that we have
|
512
|
|
- // upon unloading by invoking flush() on the connection. We flush() once
|
513
|
|
- // before disconnect() in order to attemtp to have its unavailable
|
514
|
|
- // presence at the top of the send queue. We flush() once more after
|
515
|
|
- // disconnect() in order to attempt to have its unavailable presence
|
516
|
|
- // sent as soon as possible.
|
517
|
|
- this.connection.flush();
|
518
|
|
-
|
519
|
|
- if (ev !== null && typeof ev !== 'undefined') {
|
520
|
|
- const evType = ev.type;
|
521
|
|
-
|
522
|
|
- if (evType === 'beforeunload' || evType === 'unload') {
|
523
|
|
- // XXX Whatever we said above, synchronous sending is the best
|
524
|
|
- // (known) way to properly disconnect from the XMPP server.
|
525
|
|
- // Consequently, it may be fine to have the source code and
|
526
|
|
- // comment it in or out depending on whether we want to run with
|
527
|
|
- // it for some time.
|
528
|
|
- this.connection.options.sync = true;
|
|
512
|
+ return new Promise(resolve => {
|
|
513
|
+ const disconnectListener = (credentials, status) => {
|
|
514
|
+ if (status === Strophe.Status.DISCONNECTED) {
|
|
515
|
+ resolve();
|
|
516
|
+ this.eventEmitter.removeListener(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
|
|
517
|
+ }
|
|
518
|
+ };
|
|
519
|
+
|
|
520
|
+ this.eventEmitter.on(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
|
|
521
|
+
|
|
522
|
+ // XXX Strophe is asynchronously sending by default. Unfortunately, that
|
|
523
|
+ // means that there may not be enough time to send an unavailable
|
|
524
|
+ // presence or disconnect at all. Switching Strophe to synchronous
|
|
525
|
+ // sending is not much of an option because it may lead to a noticeable
|
|
526
|
+ // delay in navigating away from the current location. As a compromise,
|
|
527
|
+ // we will try to increase the chances of sending an unavailable
|
|
528
|
+ // presence and/or disconecting within the short time span that we have
|
|
529
|
+ // upon unloading by invoking flush() on the connection. We flush() once
|
|
530
|
+ // before disconnect() in order to attemtp to have its unavailable
|
|
531
|
+ // presence at the top of the send queue. We flush() once more after
|
|
532
|
+ // disconnect() in order to attempt to have its unavailable presence
|
|
533
|
+ // sent as soon as possible.
|
|
534
|
+ this.connection.flush();
|
|
535
|
+
|
|
536
|
+ if (ev !== null && typeof ev !== 'undefined') {
|
|
537
|
+ const evType = ev.type;
|
|
538
|
+
|
|
539
|
+ if (evType === 'beforeunload' || evType === 'unload') {
|
|
540
|
+ // XXX Whatever we said above, synchronous sending is the best
|
|
541
|
+ // (known) way to properly disconnect from the XMPP server.
|
|
542
|
+ // Consequently, it may be fine to have the source code and
|
|
543
|
+ // comment it in or out depending on whether we want to run with
|
|
544
|
+ // it for some time.
|
|
545
|
+ this.connection.options.sync = true;
|
|
546
|
+ }
|
529
|
547
|
}
|
530
|
|
- }
|
531
|
548
|
|
532
|
|
- this.connection.disconnect();
|
|
549
|
+ this.connection.disconnect();
|
533
|
550
|
|
534
|
|
- if (this.connection.options.sync !== true) {
|
535
|
|
- this.connection.flush();
|
536
|
|
- }
|
|
551
|
+ if (this.connection.options.sync !== true) {
|
|
552
|
+ this.connection.flush();
|
|
553
|
+ }
|
|
554
|
+ });
|
537
|
555
|
}
|
538
|
556
|
|
539
|
557
|
/**
|