smartsocket/ts/smartsocket.classes.smartsocketclient.ts

174 lines
5.5 KiB
TypeScript
Raw Normal View History

2018-03-15 01:29:40 +00:00
import * as plugins from './smartsocket.plugins';
2019-11-06 23:26:47 +00:00
import * as interfaces from './interfaces';
2016-08-09 09:42:21 +00:00
2018-03-15 01:29:40 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection';
2019-09-09 21:58:32 +00:00
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
2018-03-19 09:00:11 +00:00
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
2019-08-12 20:31:40 +00:00
import { SocketRole } from './smartsocket.classes.socketrole';
2019-11-06 23:26:47 +00:00
import { defaultLogger } from '@pushrocks/smartlog';
2018-03-19 09:00:11 +00:00
2016-08-07 12:58:20 +00:00
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
2018-03-15 01:29:40 +00:00
port: number;
url: string;
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
2019-11-06 23:26:47 +00:00
autoReconnect?: boolean;
2016-08-07 12:58:20 +00:00
}
export class SmartsocketClient {
2019-11-06 23:26:47 +00:00
// a unique id
public shortId = plugins.smartunique.shortId();
// the shortId of the remote we connect to
public remoteShortId: string = null;
2019-08-12 20:31:40 +00:00
public alias: string;
2019-08-13 09:36:31 +00:00
public socketRole: SocketRole;
2019-08-12 20:31:40 +00:00
public socketConnection: SocketConnection;
public serverUrl: string;
public serverPort: number;
2019-11-06 23:26:47 +00:00
public autoReconnect: boolean;
2019-08-12 20:31:40 +00:00
2019-11-08 16:31:04 +00:00
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionEvent>();
2019-11-03 19:23:15 +00:00
2019-09-09 21:58:32 +00:00
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
2019-08-12 20:31:40 +00:00
public socketRoles = new plugins.lik.Objectmap<SocketRole>();
2017-07-07 20:02:19 +00:00
constructor(optionsArg: ISmartsocketClientOptions) {
2018-03-15 01:29:40 +00:00
this.alias = optionsArg.alias;
this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port;
2019-08-13 09:36:31 +00:00
this.socketRole = new SocketRole({
name: optionsArg.role,
passwordHash: optionsArg.password
});
2019-11-06 23:26:47 +00:00
this.autoReconnect = optionsArg.autoReconnect;
2017-07-07 20:02:19 +00:00
}
2016-08-11 23:32:57 +00:00
2019-09-09 21:58:32 +00:00
public addSocketFunction(socketFunction: SocketFunction<any>) {
2019-08-12 20:31:40 +00:00
this.socketFunctions.add(socketFunction);
2019-08-13 09:36:31 +00:00
this.socketRole.allowedFunctions.add(socketFunction);
2019-08-12 20:31:40 +00:00
}
2017-07-07 20:02:19 +00:00
/**
* connect the client to the server
*/
2019-08-12 20:31:40 +00:00
public connect() {
const done = plugins.smartpromise.defer();
plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
2019-08-12 20:31:40 +00:00
const socketUrl = `${this.serverUrl}:${this.serverPort}`;
2017-07-07 20:02:19 +00:00
this.socketConnection = new SocketConnection({
alias: this.alias,
authenticated: false,
2019-08-13 09:36:31 +00:00
role: this.socketRole,
2017-07-07 20:02:19 +00:00
side: 'client',
2019-08-12 20:31:40 +00:00
smartsocketHost: this,
2019-11-03 19:23:15 +00:00
socket: plugins.socketIoClient(socketUrl, {
multiplex: false,
reconnectionAttempts: 5,
})
2018-03-15 01:29:40 +00:00
});
2019-11-06 23:26:47 +00:00
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();
2019-08-13 13:58:08 +00:00
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
2019-11-06 23:26:47 +00:00
// lets register the authenticated event
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.on('authenticated', () => {
2019-11-06 23:26:47 +00:00
this.remoteShortId = requestAuthPayload.serverShortId;
2019-08-13 13:58:08 +00:00
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
2018-03-15 01:29:40 +00:00
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
2019-11-03 15:48:35 +00:00
2019-11-06 23:26:47 +00:00
// 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();
2019-11-03 15:48:35 +00:00
});
2019-11-06 23:26:47 +00:00
// lets provide the actual auth data
this.socketConnection.socket.emit('dataAuth', {
role: this.socketRole.name,
password: this.socketRole.passwordHash,
alias: this.alias
2019-11-03 15:48:35 +00:00
});
2019-11-06 23:26:47 +00:00
});
// 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();
2018-03-15 01:29:40 +00:00
});
return done.promise;
2017-07-07 20:02:19 +00:00
}
2017-10-09 08:28:18 +00:00
2019-08-13 09:36:31 +00:00
/**
* disconnect from the server
*/
public async disconnect() {
2019-11-03 15:48:35 +00:00
if (this.socketConnection) {
2019-11-06 23:26:47 +00:00
this.socketConnection.disconnect();
2019-11-03 15:48:35 +00:00
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
}
2019-11-06 23:26:47 +00:00
defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`);
this.remoteShortId = null;
2019-11-08 16:31:04 +00:00
this.eventSubject.next('terminated');
2019-11-06 23:26:47 +00:00
if (this.autoReconnect) {
this.tryDebouncedReconnect();
}
2019-11-03 15:48:35 +00:00
}
/**
* try a reconnection
*/
2019-11-03 17:33:46 +00:00
public async tryDebouncedReconnect() {
await plugins.smartdelay.delayForRandom(10000, 60000);
await this.connect();
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
2019-08-13 09:36:31 +00:00
/**
* dispatches a server call
* @param functionNameArg
* @param dataArg
*/
2019-09-09 21:58:32 +00:00
public async serverCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(functionNameArg: T['method'], dataArg: T['request']): Promise<T['response']> {
2019-08-12 20:31:40 +00:00
const done = plugins.smartpromise.defer();
2019-09-09 21:58:32 +00:00
const socketRequest = new SocketRequest<T>(this, {
2017-07-07 20:02:19 +00:00
side: 'requesting',
originSocketConnection: this.socketConnection,
2019-11-03 18:17:26 +00:00
shortId: plugins.smartunique.shortId(),
2017-07-07 20:02:19 +00:00
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
2018-03-15 01:29:40 +00:00
});
2019-08-13 09:36:31 +00:00
const response = await socketRequest.dispatch();
const result = response.funcDataArg;
return result;
2017-07-07 20:02:19 +00:00
}
}