smartsocket/ts/smartsocket.classes.smartsocketclient.ts

254 lines
7.5 KiB
TypeScript
Raw Normal View History

2018-03-15 01:29:40 +00:00
import * as plugins from './smartsocket.plugins';
2020-09-29 17:21:08 +00:00
import * as pluginsTyped from './smartsocket.pluginstyped';
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';
2020-09-24 18:04:11 +00:00
import {
ISocketFunctionCallDataRequest,
SocketFunction,
} from './smartsocket.classes.socketfunction';
2018-03-19 09:00:11 +00:00
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
2020-09-24 18:03:01 +00:00
import { logger } from './smartsocket.logging';
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
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
2020-09-29 17:21:08 +00:00
public shortId = plugins.isounique.uni();
2019-11-06 23:26:47 +00:00
// the shortId of the remote we connect to
public remoteShortId: string = null;
2019-08-12 20:31:40 +00:00
public alias: string;
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 17:41:08 +00:00
// status handling
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
2019-11-03 19:23:15 +00:00
2020-09-24 18:03:01 +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
2021-01-28 01:30:27 +00:00
// tagStore
2021-01-28 01:31:42 +00:00
private tagStore: { [key: string]: interfaces.ITag } = {};
2021-01-28 01:30:27 +00:00
private tagStoreSubscription: plugins.smartrx.rxjs.Subscription;
/**
* adds a tag to a connection
*/
public async addTag(tagArg: interfaces.ITag) {
if (this.socketConnection) {
await this.socketConnection.addTag(tagArg);
} else {
this.tagStore[tagArg.id] = tagArg;
}
}
/**
* gets a tag by id
* @param tagIdArg
*/
public async getTagById(tagIdArg: interfaces.ITag['id']) {
return this.tagStore[tagIdArg];
2021-01-28 01:31:42 +00:00
}
2021-01-28 01:30:27 +00:00
/**
* removes a tag from a connection
*/
public async removeTagById(tagIdArg: interfaces.ITag['id']) {
if (this.socketConnection) {
this.socketConnection.removeTagById(tagIdArg);
} else {
delete this.tagStore[tagIdArg];
}
}
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-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);
}
2017-07-07 20:02:19 +00:00
/**
* connect the client to the server
*/
2020-09-29 18:58:09 +00:00
public async connect() {
2019-08-12 20:31:40 +00:00
const done = plugins.smartpromise.defer();
2020-09-29 18:58:09 +00:00
const smartenvInstance = new plugins.smartenv.Smartenv();
const socketIoClient = await smartenvInstance.getEnvAwareModule({
nodeModuleName: 'socket.io-client',
webUrlArg: 'https://cdn.jsdelivr.net/npm/socket.io-client@2/dist/socket.io.js',
getFunction: () => {
return globalThis.io;
2020-09-29 19:42:38 +00:00
},
2020-09-29 18:58:09 +00:00
});
2020-09-24 18:03:01 +00:00
logger.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,
side: 'client',
2019-08-12 20:31:40 +00:00
smartsocketHost: this,
2020-09-29 18:58:09 +00:00
socket: await socketIoClient.connect(socketUrl, {
2019-11-03 19:23:15 +00:00
multiplex: false,
reconnectionAttempts: 5,
2021-01-23 05:50:02 +00:00
rejectUnauthorized: socketUrl.startsWith('https://localhost') ? false : true,
2020-09-24 18:04:11 +00:00
}),
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(() => {
2020-09-24 18:03:01 +00:00
logger.log('warn', 'connection to server timed out.');
2019-11-06 23:26:47 +00:00
this.disconnect();
});
// authentication flow
2020-09-24 18:04:11 +00:00
this.socketConnection.socket.on(
'requestAuth',
(requestAuthPayload: interfaces.IRequestAuthPayload) => {
timer.reset();
logger.log('info', 'server requested authentication');
// lets register the authenticated event
2022-01-19 06:01:58 +00:00
this.socketConnection.socket.on('authenticated', async () => {
2022-01-19 14:34:52 +00:00
this.remoteShortId = requestAuthPayload.serverAlias;
2020-09-24 18:04:11 +00:00
logger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
2022-01-19 06:01:58 +00:00
await this.socketConnection.listenToFunctionRequests();
});
this.socketConnection.socket.on('serverFullyReactive', async () => {
// lets take care of retagging
const oldTagStore = this.tagStore;
this.tagStoreSubscription?.unsubscribe();
for (const keyArg of Object.keys(this.tagStore)) {
this.socketConnection.addTag(this.tagStore[keyArg]);
}
this.tagStoreSubscription = this.socketConnection.tagStoreObservable.subscribe(
(tagStoreArg) => {
this.tagStore = tagStoreArg;
}
);
for (const tag of Object.keys(oldTagStore)) {
await this.addTag(oldTagStore[tag]);
}
2020-09-24 18:04:11 +00:00
done.resolve();
});
// lets register the forbidden event
this.socketConnection.socket.on('forbidden', async () => {
logger.log('warn', `disconnecting due to being forbidden to use the ressource`);
await this.disconnect();
});
// lets provide the actual auth data
this.socketConnection.socket.emit('dataAuth', {
alias: this.alias,
});
}
);
2019-11-06 23:26:47 +00:00
2019-11-08 17:48:39 +00:00
// handle connection
this.socketConnection.socket.on('connect', async () => {
this.updateStatus('connected');
});
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() {
2022-01-19 06:01:58 +00:00
this.tagStoreSubscription?.unsubscribe();
2019-11-03 15:48:35 +00:00
if (this.socketConnection) {
2020-09-24 18:03:01 +00:00
await this.socketConnection.disconnect();
2019-11-03 15:48:35 +00:00
this.socketConnection = undefined;
2020-09-24 18:03:01 +00:00
logger.log('ok', 'disconnected!');
2019-11-03 15:48:35 +00:00
}
2020-09-24 18:03:01 +00:00
logger.log('warn', `disconnected from server ${this.remoteShortId}`);
2019-11-06 23:26:47 +00:00
this.remoteShortId = null;
2019-11-08 17:41:08 +00:00
this.updateStatus('disconnected');
2019-11-06 23:26:47 +00:00
if (this.autoReconnect) {
this.tryDebouncedReconnect();
}
2019-11-03 15:48:35 +00:00
}
/**
* stops the client completely
*/
public async stop() {
this.autoReconnect = false;
await this.disconnect();
}
2019-11-03 15:48:35 +00:00
/**
* try a reconnection
*/
2019-11-03 17:33:46 +00:00
public async tryDebouncedReconnect() {
2022-01-19 07:05:06 +00:00
await plugins.smartdelay.delayForRandom(10000, 50000);
2019-11-03 17:33:46 +00:00
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
2020-09-24 18:04:11 +00:00
* @param functionNameArg
* @param dataArg
2019-08-13 09:36:31 +00:00
*/
2020-09-24 18:04:11 +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,
2020-09-29 17:21:08 +00:00
shortId: plugins.isounique.uni(),
2017-07-07 20:02:19 +00:00
funcCallData: {
funcName: functionNameArg,
2020-09-24 18:04:11 +00:00
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
}
2019-11-08 17:41:08 +00:00
2020-09-24 18:04:11 +00:00
private updateStatus(statusArg: interfaces.TConnectionStatus) {
2019-11-08 17:41:08 +00:00
if (this.eventStatus !== statusArg) {
this.eventSubject.next(statusArg);
}
this.eventStatus = statusArg;
}
2017-07-07 20:02:19 +00:00
}