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

145 lines
4.4 KiB
TypeScript
Raw 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';
2023-03-29 16:15:30 +02:00
import * as interfaces from './interfaces/index.js';
2016-08-07 14:58:20 +02:00
// classes
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,
2023-07-21 03:53:41 +02:00
type ISocketFunctionCallDataResponse,
2022-03-14 22:40:55 +01:00
} from './smartsocket.classes.socketfunction.js';
import { SocketRequest } from './smartsocket.classes.socketrequest.js';
import { SocketServer } from './smartsocket.classes.socketserver.js';
2018-03-15 02:29:40 +01:00
2022-03-14 22:40:55 +01:00
import { logger } from './smartsocket.logging.js';
2016-08-07 14:58:20 +02:00
export interface ISmartsocketConstructorOptions {
2022-01-19 15:34:52 +01:00
alias: string;
2019-04-24 15:47:28 +02:00
port?: number;
2017-07-07 22:02:19 +02:00
}
2016-08-07 14:58:20 +02:00
export class Smartsocket {
2019-11-03 19:17:26 +01:00
/**
* a unique id to detect server restarts
*/
2022-01-19 15:34:52 +01:00
public alias: string;
2020-09-29 17:21:08 +00:00
public smartenv = new plugins.smartenv.Smartenv();
2018-03-19 10:00:11 +01:00
public options: ISmartsocketConstructorOptions;
2020-09-24 18:03:01 +00:00
public socketConnections = new plugins.lik.ObjectMap<SocketConnection>();
public socketFunctions = new plugins.lik.ObjectMap<SocketFunction<any>>();
public socketRequests = new plugins.lik.ObjectMap<SocketRequest<any>>();
2018-03-19 10:00:11 +01:00
2023-03-29 16:15:30 +02:00
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
2023-03-29 16:07:54 +02:00
2018-03-19 10:00:11 +01:00
private socketServer = new SocketServer(this);
2018-03-15 02:29:40 +01:00
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
2022-01-19 15:34:52 +01:00
this.alias = plugins.isounique.uni(this.options.alias);
2017-07-07 22:02:19 +02:00
}
2016-08-07 14:58:20 +02:00
/**
* Returns WebSocket hooks for integration with smartserve
* Pass these hooks to SmartServe's websocket config
*/
public getSmartserveWebSocketHooks(): pluginsTyped.ISmartserveWebSocketHooks {
return this.socketServer.getSmartserveWebSocketHooks();
2019-04-24 16:09:01 +02:00
}
2018-03-19 10:00:11 +01:00
2017-07-07 22:02:19 +02:00
/**
2018-03-19 10:00:11 +01:00
* starts smartsocket
2017-07-07 22:02:19 +02:00
*/
2018-03-19 10:00:11 +01:00
public async start() {
await this.socketServer.start();
}
/**
* Handle a new WebSocket connection
* Called by SocketServer when a new connection is established
*/
public async handleNewConnection(socket: pluginsTyped.TWebSocket | pluginsTyped.IWebSocketLike) {
const socketConnection: SocketConnection = new SocketConnection({
alias: undefined,
authenticated: false,
side: 'server',
smartsocketHost: this,
socket: socket,
2018-03-15 02:29:40 +01:00
});
logger.log('info', 'Socket connected. Trying to authenticate...');
this.socketConnections.add(socketConnection);
// Handle disconnection
const handleClose = () => {
this.socketConnections.remove(socketConnection);
socketConnection.eventSubject.next('disconnected');
};
(socket as pluginsTyped.IWebSocketLike).addEventListener('close', handleClose);
(socket as pluginsTyped.IWebSocketLike).addEventListener('error', handleClose);
try {
await socketConnection.authenticate();
await socketConnection.listenToFunctionRequests();
// Signal that the server is ready
socketConnection.sendMessage({
type: 'serverReady',
payload: {},
});
} catch (err) {
logger.log('warn', `Authentication failed: ${err}`);
this.socketConnections.remove(socketConnection);
}
2017-07-07 22:02:19 +02:00
}
2017-10-09 03:06:09 +02:00
/**
2018-03-19 10:00:11 +01:00
* stops smartsocket
2017-10-09 03:06:09 +02:00
*/
2018-03-19 10:00:11 +01:00
public async stop() {
2018-03-15 02:29:40 +01:00
await plugins.smartdelay.delayFor(1000);
2019-08-13 11:36:31 +02:00
this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
2020-09-30 00:20:53 +00:00
if (socketObjectArg) {
2022-12-28 13:52:16 +01:00
logger.log(
'info',
`disconnecting socket with >>alias ${socketObjectArg.alias} due to server stop...`
);
2022-01-19 07:01:58 +01:00
socketObjectArg.disconnect();
2020-09-30 00:20:53 +00:00
}
2018-03-15 02:29:40 +01:00
});
2019-08-13 11:36:31 +02:00
this.socketConnections.wipe();
2018-03-19 10:00:11 +01:00
// stop the corresponding server
await this.socketServer.stop();
2017-07-07 22:02:19 +02:00
}
2016-08-07 15:37:52 +02:00
2017-07-07 22:02:19 +02:00
// communication
2016-08-12 01:32:57 +02:00
2017-07-07 22:02:19 +02:00
/**
* allows call to specific client.
*/
2019-09-09 23:58:32 +02:00
public async clientCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(
functionNameArg: T['method'],
dataArg: T['request'],
2018-03-19 10:00:11 +01:00
targetSocketConnectionArg: SocketConnection
2019-09-09 23:58:32 +02:00
): Promise<T['response']> {
const socketRequest = new SocketRequest<T>(this, {
2018-03-19 10:00:11 +01:00
funcCallData: {
funcDataArg: dataArg,
2020-09-24 18:04:11 +00:00
funcName: functionNameArg,
2018-03-19 10:00:11 +01:00
},
2017-07-07 22:02:19 +02:00
originSocketConnection: targetSocketConnectionArg,
2020-09-29 17:21:08 +00:00
shortId: plugins.isounique.uni(),
2020-09-24 18:04:11 +00:00
side: 'requesting',
2018-03-15 02:29:40 +01:00
});
2019-09-09 23:58:32 +02:00
const response: ISocketFunctionCallDataResponse<T> = await socketRequest.dispatch();
2019-08-13 11:36:31 +02:00
const result = response.funcDataArg;
2018-03-19 10:00:11 +01:00
return result;
2017-07-07 22:02:19 +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);
}
2017-07-07 22:02:19 +02:00
}