Skip to content

Commit

Permalink
fix: try to fix reconnect issues when using iOS, disable ping plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
trampi committed Jan 4, 2019
1 parent 9ecca74 commit 2dace50
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
3 changes: 1 addition & 2 deletions projects/pazznetwork/ngx-chat/src/lib/ngx-chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
MessagePlugin,
MessageUuidPlugin,
MultiUserChatPlugin,
PingPlugin,
PublishSubscribePlugin,
PushPlugin,
RegistrationPlugin,
Expand Down Expand Up @@ -133,7 +132,7 @@ export class NgxChatModule {
new RosterPlugin(xmppChatAdapter, logService),
new ServiceDiscoveryPlugin(xmppChatAdapter),
new PushPlugin(xmppChatAdapter),
new PingPlugin(xmppChatAdapter, logService, ngZone),
// new PingPlugin(xmppChatAdapter, logService, ngZone),
new RegistrationPlugin(logService, ngZone),
unreadMessageCountPlugin,
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NgZone } from '@angular/core';
import { timeout } from '@xmpp/events';
import { x as xml } from '@xmpp/xml';
import { filter } from 'rxjs/operators';
import { LogService } from '../../../log.service';
Expand Down Expand Up @@ -41,15 +42,27 @@ export class PingPlugin extends AbstractXmppPlugin {
private async ping() {
this.logService.debug('ping...');
try {
await this.xmppChatAdapter.chatConnectionService.sendIq(
await timeout(this.sendPing(), 10_000);
this.logService.debug('... pong');
} catch (e) {
if (this.xmppChatAdapter.state$.getValue() === 'online'
&& this.xmppChatAdapter.chatConnectionService.state$.getValue() === 'online') {
this.logService.error('... pong errored, it thinks it is online, scheduling reconnect!');
// TODO: state handling necessary! we have to prevent two reconnects at the same time!
this.xmppChatAdapter.reconnect();
}
}
}

private sendPing() {
try {
return this.xmppChatAdapter.chatConnectionService.sendIq(
xml('iq', {type: 'get'},
xml('ping', {xmlns: 'urn:xmpp:ping'})
)
);
// TODO: timeout?
this.logService.debug('... pong');
} catch (e) {
this.logService.error('... pong errored!');
return Promise.reject(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ export class XmppChatConnectionService {
initialize(): void {
this.client.on('error', (err: any) => {
this.ngZone.run(() => {
this.logService.error('chat service error =>', err.toString());
this.client.stop();
this.logService.error('chat service error =>', err.toString(), err);
if (err.toString().indexOf('connection error ') >= 0) { // thrown by iOS when gone offline due to battery saving
this.reconnect();
} else {
this.client.stop(); // e.g. kicked
}
});
});

this.client.on('status', (status: any, value: any) => {
this.ngZone.run(() => {
this.logService.info('status update =', status, value ? value.toString() : '');
this.logService.info('status update =', status, value ? JSON.stringify(value) : '');
if (status === 'offline') {
this.state$.next('disconnected');
}
Expand All @@ -62,10 +66,15 @@ export class XmppChatConnectionService {
});
});

this.client.plugins.reconnect.on('reconnected', () => {
this.ngZone.run(() => {
this.sendPresence();
});
});
}

public onOnline(jid: JID) {
this.logService.debug('online =', 'online as', jid.toString());
this.logService.info('online =', 'online as', jid.toString());
this.userJid = jid;
this.state$.next('online');
}
Expand All @@ -78,7 +87,11 @@ export class XmppChatConnectionService {

public send(content: any): PromiseLike<void> {
this.logService.debug('>>>', content);
return this.client.send(content);
try {
return this.client.send(content);
} catch (e) {
return Promise.reject(e);
}
}

public sendIq(request: Element): Promise<IqResponseStanza> {
Expand Down Expand Up @@ -165,6 +178,8 @@ export class XmppChatConnectionService {
}

reconnect() {
this.logService.warn('hard reconnect...');
this.state$.next('disconnected');
this.client.plugins.reconnect.reconnect();
}
}

0 comments on commit 2dace50

Please sign in to comment.