smartsocket/ts/smartsocket.classes.smartsocketclient.ts

267 lines
8.1 KiB
TypeScript
Raw Normal View History

2022-03-14 21:40:55 +00:00
import * as plugins from './smartsocket.plugins.js';
import * as pluginsTyped from './smartsocket.pluginstyped.js';
import * as interfaces from './interfaces/index.js';
2016-08-09 09:42:21 +00:00
2022-03-14 21:40:55 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection.js';
2020-09-24 18:04:11 +00:00
import {
2023-07-21 01:53:41 +00:00
type ISocketFunctionCallDataRequest,
2020-09-24 18:04:11 +00:00
SocketFunction,
2022-03-14 21:40:55 +00:00
} from './smartsocket.classes.socketfunction.js';
2023-07-21 01:53:41 +00:00
import { type ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest.js';
2022-03-14 21:40:55 +00:00
import { logger } from './smartsocket.logging.js';
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();
2022-01-19 17:36:13 +00:00
const socketIoClient: any = await smartenvInstance.getEnvAwareModule({
nodeModuleName: 'socket.io-client',
webUrlArg: 'https://cdn.jsdelivr.net/npm/socket.io-client@4/dist/socket.io.js',
getFunction: () => {
2022-12-28 12:51:40 +00:00
const socketIoBrowserModule = (globalThis as any).io;
2022-12-29 12:28:11 +00:00
// console.log('loaded socket.io for browser');
2022-12-28 12:51:40 +00:00
return socketIoBrowserModule;
2022-01-19 17:36:13 +00:00
},
});
2022-12-29 12:28:11 +00:00
// console.log(socketIoClient);
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,
2022-01-20 15:50:25 +00:00
socket: await socketIoClient
.connect(socketUrl, {
2023-03-20 17:51:02 +00:00
multiplex: true,
rememberUpgrade: true,
2022-01-20 15:50:25 +00:00
autoConnect: false,
reconnectionAttempts: 0,
rejectUnauthorized: socketUrl.startsWith('https://localhost') ? false : true,
})
.open(),
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(() => {
2022-01-20 17:33:46 +00:00
this.updateStatus('timedOut');
2020-09-24 18:03:01 +00:00
logger.log('warn', 'connection to server timed out.');
2022-01-20 15:50:25 +00:00
this.disconnect(true);
2019-11-06 23:26:47 +00:00
});
// authentication flow
2022-01-20 15:50:25 +00:00
this.socketConnection.socket.on('requestAuth', (dataArg: interfaces.IRequestAuthPayload) => {
timer.reset();
logger.log('info', `server ${dataArg.serverAlias} requested authentication`);
// lets register the authenticated event
this.socketConnection.socket.on('authenticated', async () => {
this.remoteShortId = dataArg.serverAlias;
logger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
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;
2022-01-19 06:01:58 +00:00
}
2022-01-20 15:50:25 +00:00
);
2019-11-06 23:26:47 +00:00
2022-01-20 15:50:25 +00:00
for (const tag of Object.keys(oldTagStore)) {
await this.addTag(oldTagStore[tag]);
}
this.updateStatus('connected');
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-08 17:48:39 +00:00
});
2022-01-20 15:50:25 +00:00
// handle connection
this.socketConnection.socket.on('connect', async () => {});
2019-11-06 23:26:47 +00:00
// handle disconnection and errors
this.socketConnection.socket.on('disconnect', async () => {
2022-01-20 15:50:25 +00:00
await this.disconnect(true);
2019-11-06 23:26:47 +00:00
});
this.socketConnection.socket.on('reconnect_failed', async () => {
2022-01-20 15:50:25 +00:00
await this.disconnect(true);
2019-11-06 23:26:47 +00:00
});
this.socketConnection.socket.on('connect_error', async () => {
2022-01-20 15:50:25 +00:00
await this.disconnect(true);
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
2022-01-20 16:14:11 +00:00
private disconnectRunning = false;
2019-08-13 09:36:31 +00:00
/**
* disconnect from the server
*/
2022-01-20 17:33:46 +00:00
public async disconnect(useAutoReconnectSetting = false) {
2022-01-20 16:14:11 +00:00
if (this.disconnectRunning) {
2022-01-20 15:50:25 +00:00
return;
}
2022-01-20 16:14:11 +00:00
this.disconnectRunning = true;
2022-01-20 15:50:25 +00:00
this.updateStatus('disconnecting');
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;
2022-01-20 15:50:25 +00:00
logger.log('ok', 'disconnected socket!');
2022-01-20 16:14:11 +00:00
} else {
this.disconnectRunning = false;
logger.log('warn', 'tried to disconnect, without a SocketConnection');
return;
2019-11-03 15:48:35 +00:00
}
2022-01-20 16:14:11 +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;
2022-01-20 17:33:46 +00:00
if (this.autoReconnect && useAutoReconnectSetting && this.eventStatus !== 'connecting') {
2022-01-20 15:50:25 +00:00
this.updateStatus('connecting');
2022-01-20 17:38:17 +00:00
console.log('debounced reconnect!');
await plugins.smartdelay.delayForRandom(10000, 20000);
2022-01-20 16:14:11 +00:00
this.disconnectRunning = false;
2022-01-20 17:38:17 +00:00
await this.connect();
2022-01-20 16:14:11 +00:00
} else {
this.disconnectRunning = false;
2019-11-06 23:26:47 +00:00
}
2019-11-03 15:48:35 +00:00
}
/**
* stops the client completely
*/
public async stop() {
this.autoReconnect = false;
await this.disconnect();
}
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
}