fix(core): update

This commit is contained in:
2019-11-07 00:26:47 +01:00
parent 20e7584eb9
commit bcc4ce9a87
6 changed files with 79 additions and 18 deletions

View File

@@ -1,9 +1,11 @@
import * as plugins from './smartsocket.plugins';
import * as interfaces from './interfaces';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
import { defaultLogger } from '@pushrocks/smartlog';
/**
* interface for class SmartsocketClient
@@ -14,16 +16,24 @@ export interface ISmartsocketClientOptions {
alias: string; // an alias makes it easier to identify this client in a multo client environment
role: string;
password: string; // by setting a password access to functions can be limited
autoReconnect?: boolean;
}
export class SmartsocketClient {
// a unique id
public shortId = plugins.smartunique.shortId();
// the shortId of the remote we connect to
public remoteShortId: string = null;
public alias: string;
public socketRole: SocketRole;
public socketConnection: SocketConnection;
public serverUrl: string;
public serverPort: number;
public autoReconnect: boolean;
// public eventSubject = new plugins.smart
public eventSubject = new plugins.smartrx.rxjs.Subject();
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
@@ -37,6 +47,7 @@ export class SmartsocketClient {
name: optionsArg.role,
passwordHash: optionsArg.password
});
this.autoReconnect = optionsArg.autoReconnect;
}
public addSocketFunction(socketFunction: SocketFunction<any>) {
@@ -62,27 +73,53 @@ export class SmartsocketClient {
reconnectionAttempts: 5,
})
});
this.socketConnection.socket.on('requestAuth', () => {
const timer = new plugins.smarttime.Timer(5000);
timer.start();
timer.completed.then(() => {
defaultLogger.log('warn', 'connection to server timed out.');
this.disconnect();
});
// authentication flow
this.socketConnection.socket.on('requestAuth', (requestAuthPayload: interfaces.IRequestAuthPayload) => {
timer.reset();
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
this.socketConnection.socket.emit('dataAuth', {
role: this.socketRole.name,
password: this.socketRole.passwordHash,
alias: this.alias
});
// lets register the authenticated event
this.socketConnection.socket.on('authenticated', () => {
this.remoteShortId = requestAuthPayload.serverShortId;
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
// handle errors
this.socketConnection.socket.on('reconnect_failed', async () => {
this.disconnect();
// lets register the forbidden event
this.socketConnection.socket.on('forbidden', async () => {
defaultLogger.log('warn', `disconnecting due to being forbidden to use the ressource`);
await this.disconnect();
});
this.socketConnection.socket.on('connect_error', async () => {
this.disconnect();
// lets provide the actual auth data
this.socketConnection.socket.emit('dataAuth', {
role: this.socketRole.name,
password: this.socketRole.passwordHash,
alias: this.alias
});
});
// handle disconnection and errors
this.socketConnection.socket.on('disconnect', async () => {
await this.disconnect();
});
this.socketConnection.socket.on('reconnect_failed', async () => {
await this.disconnect();
});
this.socketConnection.socket.on('connect_error', async () => {
await this.disconnect();
});
return done.promise;
}
@@ -92,10 +129,16 @@ export class SmartsocketClient {
*/
public async disconnect() {
if (this.socketConnection) {
this.socketConnection.socket.disconnect(true);
this.socketConnection.disconnect();
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
}
defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`);
this.remoteShortId = null;
if (this.autoReconnect) {
this.tryDebouncedReconnect();
}
}
/**