smartsocket/ts/smartsocket.classes.smartsocket.ts

127 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-03-15 01:29:40 +00:00
import * as plugins from './smartsocket.plugins';
2016-08-07 12:58:20 +00:00
// classes
2018-03-15 01:29:40 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection';
2019-09-09 21:58:32 +00:00
import { ISocketFunctionCallDataRequest, SocketFunction, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
2018-03-15 01:29:40 +00:00
import { SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
2018-03-19 09:00:11 +00:00
import { SocketServer } from './smartsocket.classes.socketserver';
2018-03-15 01:29:40 +00:00
// socket.io
import * as SocketIO from 'socket.io';
2020-09-24 18:03:01 +00:00
import { logger } from './smartsocket.logging';
2016-08-07 12:58:20 +00:00
export interface ISmartsocketConstructorOptions {
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
*/
2019-11-06 23:26:47 +00:00
public shortId = plugins.smartunique.shortId();
2018-03-19 09:00:11 +00:00
public options: ISmartsocketConstructorOptions;
public io: SocketIO.Server;
2020-09-24 18:03:01 +00:00
public socketConnections = new plugins.lik.ObjectMap<SocketConnection>();
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
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;
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() {
this.io = plugins.socketIo(this.socketServer.getServerForSocketIo());
await this.socketServer.start();
2018-03-15 01:29:40 +00:00
this.io.on('connection', socketArg => {
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-24 18:03:01 +00:00
logger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
2018-03-15 01:29:40 +00:00
socketObjectArg.socket.disconnect();
});
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,
funcName: functionNameArg
},
2017-07-07 20:02:19 +00:00
originSocketConnection: targetSocketConnectionArg,
2019-11-03 18:17:26 +00:00
shortId: plugins.smartunique.shortId(),
2018-03-19 09:00: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
}
2017-07-07 20:02:19 +00:00
/**
* adds socketRoles
*/
2018-03-19 09:00:11 +00:00
public addSocketRoles(socketRolesArray: SocketRole[]): void {
for (const socketRole of socketRolesArray) {
2018-03-15 01:29:40 +00:00
this.socketRoles.add(socketRole);
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
return;
2017-07-07 20:02:19 +00:00
}
2016-09-04 22:34:09 +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
*/
2019-08-13 09:36:31 +00:00
private async _handleSocketConnection(socketArg: plugins.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,
role: undefined,
side: 'server',
smartsocketHost: this,
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);
await socketConnection.authenticate();
await socketConnection.listenToFunctionRequests();
2017-07-07 20:02:19 +00:00
}
}