diff --git a/projects/pazznetwork/ngx-chat/src/lib/ngx-chat.module.ts b/projects/pazznetwork/ngx-chat/src/lib/ngx-chat.module.ts index 695339f6..ed5d4d9f 100644 --- a/projects/pazznetwork/ngx-chat/src/lib/ngx-chat.module.ts +++ b/projects/pazznetwork/ngx-chat/src/lib/ngx-chat.module.ts @@ -25,7 +25,6 @@ import { MessagePlugin, MessageUuidPlugin, MultiUserChatPlugin, - PingPlugin, PublishSubscribePlugin, PushPlugin, RegistrationPlugin, @@ -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, ]); diff --git a/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/plugins/ping.plugin.ts b/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/plugins/ping.plugin.ts index 144f234d..49583733 100644 --- a/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/plugins/ping.plugin.ts +++ b/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/plugins/ping.plugin.ts @@ -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'; @@ -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); } } diff --git a/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/xmpp-chat-connection.service.ts b/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/xmpp-chat-connection.service.ts index 528576cb..38f51f1b 100644 --- a/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/xmpp-chat-connection.service.ts +++ b/projects/pazznetwork/ngx-chat/src/lib/services/adapters/xmpp/xmpp-chat-connection.service.ts @@ -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'); } @@ -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'); } @@ -78,7 +87,11 @@ export class XmppChatConnectionService { public send(content: any): PromiseLike { 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 { @@ -165,6 +178,8 @@ export class XmppChatConnectionService { } reconnect() { + this.logService.warn('hard reconnect...'); + this.state$.next('disconnected'); this.client.plugins.reconnect.reconnect(); } }