Files
smartsocket/ts/smartsocket.classes.smartsocketclient.ts

379 lines
12 KiB
TypeScript
Raw Permalink Normal View History

2022-03-14 22:40:55 +01: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 11:42:21 +02:00
2022-03-14 22:40:55 +01:00
import { SocketConnection } from './smartsocket.classes.socketconnection.js';
2020-09-24 18:04:11 +00:00
import {
2023-07-21 03:53:41 +02:00
type ISocketFunctionCallDataRequest,
2020-09-24 18:04:11 +00:00
SocketFunction,
2022-03-14 22:40:55 +01:00
} from './smartsocket.classes.socketfunction.js';
2023-07-21 03:53:41 +02:00
import { type ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest.js';
2022-03-14 22:40:55 +01:00
import { logger } from './smartsocket.logging.js';
2018-03-19 10:00:11 +01:00
2016-08-07 14:58:20 +02:00
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
2018-03-15 02:29:40 +01:00
port: number;
url: string;
alias: string; // an alias makes it easier to identify this client in a multi client environment
2019-11-07 00:26:47 +01:00
autoReconnect?: boolean;
maxRetries?: number; // maximum number of reconnection attempts
initialBackoffDelay?: number; // initial backoff delay in ms
maxBackoffDelay?: number; // maximum backoff delay in ms
2016-08-07 14:58:20 +02:00
}
export class SmartsocketClient {
2019-11-07 00:26:47 +01:00
// a unique id
2020-09-29 17:21:08 +00:00
public shortId = plugins.isounique.uni();
2019-11-07 00:26:47 +01:00
// the shortId of the remote we connect to
public remoteShortId: string = null;
2019-08-12 22:31:40 +02:00
public alias: string;
public socketConnection: SocketConnection;
public serverUrl: string;
public serverPort: number;
2019-11-07 00:26:47 +01:00
public autoReconnect: boolean;
public maxRetries: number;
public initialBackoffDelay: number;
public maxBackoffDelay: number;
public currentRetryCount = 0;
public currentBackoffDelay: number;
2019-08-12 22:31:40 +02:00
2019-11-08 18:41:08 +01:00
// status handling
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
2019-11-03 20:23:15 +01: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 22:31:40 +02: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 22:02:19 +02:00
constructor(optionsArg: ISmartsocketClientOptions) {
2018-03-15 02:29:40 +01:00
this.alias = optionsArg.alias;
this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port;
2019-11-07 00:26:47 +01:00
this.autoReconnect = optionsArg.autoReconnect;
this.maxRetries = optionsArg.maxRetries ?? 100; // Default to 100 retries
this.initialBackoffDelay = optionsArg.initialBackoffDelay ?? 1000; // Default to 1 second
this.maxBackoffDelay = optionsArg.maxBackoffDelay ?? 60000; // Default to 1 minute
this.currentBackoffDelay = this.initialBackoffDelay;
2017-07-07 22:02:19 +02:00
}
2016-08-12 01:32:57 +02:00
2019-09-09 23:58:32 +02:00
public addSocketFunction(socketFunction: SocketFunction<any>) {
2019-08-12 22:31:40 +02:00
this.socketFunctions.add(socketFunction);
}
private isReconnecting = false;
private isConnecting = false;
2017-07-07 22:02:19 +02:00
/**
* connect the client to the server
*/
2020-09-29 18:58:09 +00:00
public async connect() {
// Prevent duplicate connection attempts
if (this.isConnecting) {
return;
}
this.isConnecting = true;
// Only reset retry counters on fresh connection (not during auto-reconnect)
if (!this.isReconnecting) {
this.currentRetryCount = 0;
this.currentBackoffDelay = this.initialBackoffDelay;
}
this.isReconnecting = false;
2019-08-12 22:31:40 +02:00
const done = plugins.smartpromise.defer();
2020-09-29 18:58:09 +00:00
const smartenvInstance = new plugins.smartenv.Smartenv();
2020-09-24 18:03:01 +00:00
logger.log('info', 'trying to connect...');
// Construct WebSocket URL
const protocol = this.serverUrl.startsWith('https') ? 'wss' : 'ws';
const host = this.serverUrl.replace(/^https?:\/\//, '');
const socketUrl = `${protocol}://${host}:${this.serverPort}`;
// Get WebSocket implementation (native in browser, ws in Node)
let WebSocketClass: typeof WebSocket;
if (typeof WebSocket !== 'undefined') {
// Browser environment
WebSocketClass = WebSocket;
} else {
// Node.js environment
const wsModule = await smartenvInstance.getSafeNodeModule('ws');
WebSocketClass = wsModule.default || wsModule;
}
const socket = new WebSocketClass(socketUrl);
this.currentSocket = socket;
2017-07-07 22:02:19 +02:00
this.socketConnection = new SocketConnection({
alias: this.alias,
authenticated: false,
side: 'client',
2019-08-12 22:31:40 +02:00
smartsocketHost: this,
socket: socket as any,
2018-03-15 02:29:40 +01:00
});
2019-11-07 00:26:47 +01:00
// Increment attempt ID to invalidate any pending timers from previous attempts
this.connectionAttemptId++;
const currentAttemptId = this.connectionAttemptId;
2019-11-07 00:26:47 +01:00
const timer = new plugins.smarttime.Timer(5000);
timer.start();
timer.completed.then(() => {
// Only fire timeout if this is still the current connection attempt
if (currentAttemptId === this.connectionAttemptId && this.eventStatus !== 'connected') {
this.updateStatus('timedOut');
logger.log('warn', 'connection to server timed out.');
this.disconnect(true);
}
2019-11-07 00:26:47 +01:00
});
// Handle connection open
socket.addEventListener('open', () => {
2022-01-20 16:50:25 +01:00
timer.reset();
});
// Handle messages
socket.addEventListener('message', async (event: MessageEvent | { data: string }) => {
try {
const data = typeof event.data === 'string' ? event.data : event.data.toString();
const message: interfaces.ISocketMessage = JSON.parse(data);
switch (message.type) {
case 'authRequest':
timer.reset();
const authRequestPayload = message.payload as interfaces.IAuthRequestPayload;
logger.log('info', `server ${authRequestPayload.serverAlias} requested authentication`);
this.remoteShortId = authRequestPayload.serverAlias;
// Send authentication data
this.socketConnection.sendMessage({
type: 'auth',
payload: { alias: this.alias },
});
break;
2019-11-07 00:26:47 +01:00
case 'authResponse':
const authResponse = message.payload as interfaces.IAuthResponsePayload;
if (authResponse.success) {
logger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
} else {
logger.log('warn', `authentication failed: ${authResponse.error}`);
await this.disconnect();
}
break;
case 'serverReady':
// Set up function request listening
await this.socketConnection.listenToFunctionRequests();
// Handle 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]);
}
this.updateStatus('connected');
this.isConnecting = false;
done.resolve();
break;
default:
// Other messages are handled by SocketConnection
this.socketConnection.handleMessage(message);
break;
2022-01-20 16:50:25 +01:00
}
} catch (err) {
// Not a valid JSON message, ignore
}
2019-11-08 18:48:39 +01:00
});
// Handle disconnection and errors
const closeHandler = async () => {
// Only handle close if this is still the current socket and we're not already disconnecting
if (this.currentSocket === socket && !this.disconnectRunning) {
logger.log(
'info',
`SocketConnection with >alias ${this.alias} on >side client disconnected`
);
await this.disconnect(true);
}
};
const errorHandler = async () => {
if (this.currentSocket === socket && !this.disconnectRunning) {
await this.disconnect(true);
}
};
2022-01-20 16:50:25 +01:00
socket.addEventListener('close', closeHandler);
socket.addEventListener('error', errorHandler);
2019-11-07 00:26:47 +01:00
2018-03-15 02:29:40 +01:00
return done.promise;
2017-07-07 22:02:19 +02:00
}
2017-10-09 10:28:18 +02:00
2022-01-20 17:14:11 +01:00
private disconnectRunning = false;
private currentSocket: WebSocket | null = null;
private connectionAttemptId = 0; // Increment on each connect attempt to invalidate old timers
2022-01-20 17:14:11 +01:00
2019-08-13 11:36:31 +02:00
/**
* disconnect from the server
*/
2022-01-20 18:33:46 +01:00
public async disconnect(useAutoReconnectSetting = false) {
2022-01-20 17:14:11 +01:00
if (this.disconnectRunning) {
2022-01-20 16:50:25 +01:00
return;
}
2022-01-20 17:14:11 +01:00
this.disconnectRunning = true;
this.isConnecting = false;
2022-01-20 16:50:25 +01:00
this.updateStatus('disconnecting');
2022-01-19 07:01:58 +01:00
this.tagStoreSubscription?.unsubscribe();
// Store reference to current socket before cleanup
const socketToClose = this.currentSocket;
this.currentSocket = null;
2019-11-03 16:48:35 +01:00
if (this.socketConnection) {
2020-09-24 18:03:01 +00:00
await this.socketConnection.disconnect();
2019-11-03 16:48:35 +01:00
this.socketConnection = undefined;
2022-01-20 16:50:25 +01:00
logger.log('ok', 'disconnected socket!');
} else if (!socketToClose) {
2022-01-20 17:14:11 +01:00
this.disconnectRunning = false;
logger.log('warn', 'tried to disconnect, without a SocketConnection');
return;
2019-11-03 16:48:35 +01:00
}
2022-01-20 17:14:11 +01:00
2020-09-24 18:03:01 +00:00
logger.log('warn', `disconnected from server ${this.remoteShortId}`);
2019-11-07 00:26:47 +01:00
this.remoteShortId = null;
2022-01-20 18:33:46 +01:00
if (this.autoReconnect && useAutoReconnectSetting && this.eventStatus !== 'connecting') {
2022-01-20 16:50:25 +01:00
this.updateStatus('connecting');
// Check if we've exceeded the maximum number of retries
if (this.currentRetryCount >= this.maxRetries) {
logger.log('warn', `Maximum reconnection attempts (${this.maxRetries}) reached. Giving up.`);
this.disconnectRunning = false;
return;
}
// Increment retry counter
this.currentRetryCount++;
// Calculate backoff with jitter (±20% randomness)
const jitter = this.currentBackoffDelay * 0.2 * (Math.random() * 2 - 1);
const delay = Math.min(this.currentBackoffDelay + jitter, this.maxBackoffDelay);
logger.log('info', `Reconnect attempt ${this.currentRetryCount}/${this.maxRetries} in ${Math.round(delay)}ms`);
// Apply exponential backoff for next time (doubling with each attempt)
this.currentBackoffDelay = Math.min(this.currentBackoffDelay * 2, this.maxBackoffDelay);
await plugins.smartdelay.delayFor(delay);
2022-01-20 17:14:11 +01:00
this.disconnectRunning = false;
this.isReconnecting = true;
2022-01-20 18:38:17 +01:00
await this.connect();
2022-01-20 17:14:11 +01:00
} else {
this.disconnectRunning = false;
2019-11-07 00:26:47 +01:00
}
2019-11-03 16:48:35 +01:00
}
/**
* stops the client completely
*/
public async stop() {
this.autoReconnect = false;
this.currentRetryCount = 0;
this.currentBackoffDelay = this.initialBackoffDelay;
await this.disconnect();
}
2019-08-13 11:36:31 +02:00
/**
* dispatches a server call
2020-09-24 18:04:11 +00:00
* @param functionNameArg
* @param dataArg
2019-08-13 11:36:31 +02: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-09-09 23:58:32 +02:00
const socketRequest = new SocketRequest<T>(this, {
2017-07-07 22:02:19 +02:00
side: 'requesting',
originSocketConnection: this.socketConnection,
2020-09-29 17:21:08 +00:00
shortId: plugins.isounique.uni(),
2017-07-07 22:02:19 +02:00
funcCallData: {
funcName: functionNameArg,
2020-09-24 18:04:11 +00:00
funcDataArg: dataArg,
},
2018-03-15 02:29:40 +01:00
});
2019-08-13 11:36:31 +02:00
const response = await socketRequest.dispatch();
const result = response.funcDataArg;
return result;
2017-07-07 22:02:19 +02:00
}
2019-11-08 18:41:08 +01:00
2020-09-24 18:04:11 +00:00
private updateStatus(statusArg: interfaces.TConnectionStatus) {
2019-11-08 18:41:08 +01:00
if (this.eventStatus !== statusArg) {
this.eventSubject.next(statusArg);
}
this.eventStatus = statusArg;
// Reset reconnection state when connection is successful
if (statusArg === 'connected') {
this.currentRetryCount = 0;
this.currentBackoffDelay = this.initialBackoffDelay;
}
}
/**
* Resets the reconnection state
*/
public resetReconnectionState() {
this.currentRetryCount = 0;
this.currentBackoffDelay = this.initialBackoffDelay;
2019-11-08 18:41:08 +01:00
}
2017-07-07 22:02:19 +02:00
}