smartsocket/ts/smartsocket.classes.smartsocket.ts

130 lines
4.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';
2016-08-07 12:58:20 +00:00
// classes
2022-03-14 21:40:55 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection.js';
2020-09-24 18:04:11 +00:00
import {
ISocketFunctionCallDataRequest,
SocketFunction,
ISocketFunctionCallDataResponse,
2022-03-14 21:40:55 +00:00
} from './smartsocket.classes.socketfunction.js';
import { SocketRequest } from './smartsocket.classes.socketrequest.js';
import { SocketServer } from './smartsocket.classes.socketserver.js';
2018-03-15 01:29:40 +00:00
2022-03-14 21:40:55 +00:00
import { logger } from './smartsocket.logging.js';
2016-08-07 12:58:20 +00:00
export interface ISmartsocketConstructorOptions {
2022-01-19 14:34:52 +00:00
alias: string;
2019-04-24 13:47:28 +00:00
port?: number;
2017-07-07 20:02:19 +00:00
}
2016-08-07 12:58:20 +00:00
export class Smartsocket {
2019-11-03 18:17:26 +00:00
/**
* a unique id to detect server restarts
*/
2022-01-19 14:34:52 +00:00
public alias: string;
2020-09-29 17:21:08 +00:00
public smartenv = new plugins.smartenv.Smartenv();
2018-03-19 09:00:11 +00:00
public options: ISmartsocketConstructorOptions;
2020-09-29 17:21:08 +00:00
public io: pluginsTyped.socketIo.Server;
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 09:00:11 +00:00
private socketServer = new SocketServer(this);
2018-03-15 01:29:40 +00:00
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
2022-01-19 14:34:52 +00:00
this.alias = plugins.isounique.uni(this.options.alias);
2017-07-07 20:02:19 +00:00
}
2016-08-07 12:58:20 +00:00
2018-03-19 09:00:11 +00:00
// tslint:disable-next-line:member-ordering
2019-04-26 15:35:15 +00:00
public async setExternalServer(serverType: 'smartexpress', serverArg: any) {
2019-04-24 14:09:01 +00:00
await this.socketServer.setExternalServer(serverType, serverArg);
}
2018-03-19 09:00:11 +00:00
2017-07-07 20:02:19 +00:00
/**
2018-03-19 09:00:11 +00:00
* starts smartsocket
2017-07-07 20:02:19 +00:00
*/
2018-03-19 09:00:11 +00:00
public async start() {
2022-03-24 11:52:28 +00:00
const socketIoModule = await this.smartenv.getSafeNodeModule('socket.io');
this.io = new socketIoModule.Server(await this.socketServer.getServerForSocketIo());
2018-03-19 09:00:11 +00:00
await this.socketServer.start();
2020-09-24 18:04:11 +00:00
this.io.on('connection', (socketArg) => {
2018-03-15 01:29:40 +00:00
this._handleSocketConnection(socketArg);
});
2017-07-07 20:02:19 +00:00
}
2017-10-09 01:06:09 +00:00
/**
2018-03-19 09:00:11 +00:00
* stops smartsocket
2017-10-09 01:06:09 +00:00
*/
2018-03-19 09:00:11 +00:00
public async stop() {
2018-03-15 01:29:40 +00:00
await plugins.smartdelay.delayFor(1000);
2019-08-13 09:36:31 +00:00
this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
2020-09-30 00:20:53 +00:00
if (socketObjectArg) {
2022-12-28 12:52:16 +00:00
logger.log(
'info',
`disconnecting socket with >>alias ${socketObjectArg.alias} due to server stop...`
);
2022-01-19 06:01:58 +00:00
socketObjectArg.disconnect();
2020-09-30 00:20:53 +00:00
}
2018-03-15 01:29:40 +00:00
});
2019-08-13 09:36:31 +00:00
this.socketConnections.wipe();
2018-03-15 01:29:40 +00:00
this.io.close();
2018-03-19 09:00:11 +00:00
// stop the corresponging server
this.socketServer.stop();
2017-07-07 20:02:19 +00:00
}
2016-08-07 13:37:52 +00:00
2017-07-07 20:02:19 +00:00
// communication
2016-08-11 23:32:57 +00:00
2017-07-07 20:02:19 +00:00
/**
* allows call to specific client.
*/
2019-09-09 21:58:32 +00:00
public async clientCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(
functionNameArg: T['method'],
dataArg: T['request'],
2018-03-19 09:00:11 +00:00
targetSocketConnectionArg: SocketConnection
2019-09-09 21:58:32 +00:00
): Promise<T['response']> {
const socketRequest = new SocketRequest<T>(this, {
2018-03-19 09:00:11 +00:00
funcCallData: {
funcDataArg: dataArg,
2020-09-24 18:04:11 +00:00
funcName: functionNameArg,
2018-03-19 09:00:11 +00:00
},
2017-07-07 20:02:19 +00: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 01:29:40 +00:00
});
2019-09-09 21:58:32 +00:00
const response: ISocketFunctionCallDataResponse<T> = await socketRequest.dispatch();
2019-08-13 09:36:31 +00:00
const result = response.funcDataArg;
2018-03-19 09:00:11 +00:00
return result;
2017-07-07 20:02:19 +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
/**
* the standard handler for new socket connections
*/
2022-01-19 17:06:39 +00:00
private async _handleSocketConnection(socketArg: pluginsTyped.socketIo.Socket) {
2018-03-19 09:00:11 +00:00
const socketConnection: SocketConnection = new SocketConnection({
2017-07-07 20:02:19 +00:00
alias: undefined,
authenticated: false,
side: 'server',
smartsocketHost: this,
2020-09-24 18:04:11 +00:00
socket: socketArg,
2018-03-15 01:29:40 +00:00
});
2020-09-24 18:03:01 +00:00
logger.log('info', 'Socket connected. Trying to authenticate...');
2019-08-13 09:36:31 +00:00
this.socketConnections.add(socketConnection);
2022-01-19 06:01:58 +00:00
const disconnectSubscription = socketConnection.eventSubject.subscribe((eventArg) => {
if (eventArg === 'disconnected') {
this.socketConnections.remove(socketConnection);
disconnectSubscription.unsubscribe();
}
});
2019-08-13 09:36:31 +00:00
await socketConnection.authenticate();
await socketConnection.listenToFunctionRequests();
2022-01-19 06:01:58 +00:00
await socketConnection.socket.emit('serverFullyReactive');
2017-07-07 20:02:19 +00:00
}
}