smartsocket/ts/smartsocket.classes.smartsocket.ts

125 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-03-15 01:29:40 +00:00
import * as plugins from './smartsocket.plugins';
import * as helpers from './smartsocket.helpers';
2016-08-07 12:58:20 +00:00
// classes
import { Objectmap } from '@pushrocks/lik';
2018-03-15 01:29:40 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection';
2018-03-19 09:00:11 +00:00
import { ISocketFunctionCall, SocketFunction } 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';
2016-08-07 12:58:20 +00:00
export interface ISmartsocketConstructorOptions {
2018-03-15 01:29:40 +00:00
port: number;
2017-07-07 20:02:19 +00:00
}
2016-08-07 12:58:20 +00:00
export class Smartsocket {
2018-03-19 09:00:11 +00:00
public options: ISmartsocketConstructorOptions;
public io: SocketIO.Server;
public openSockets = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>();
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
public setExternalServer = this.socketServer.setExternalServer;
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);
2017-07-07 20:02:19 +00:00
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
2018-03-15 01:29:40 +00:00
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
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.
*/
2018-03-19 09:00:11 +00:00
public async clientCall(
functionNameArg: string,
dataArg: any,
targetSocketConnectionArg: SocketConnection
) {
const done = plugins.smartpromise.defer();
2018-03-19 09:00:11 +00:00
const socketRequest = new SocketRequest({
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
2017-07-07 20:02:19 +00:00
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
2018-03-19 09:00:11 +00:00
side: 'requesting'
2018-03-15 01:29:40 +00:00
});
socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg);
});
2018-03-19 09:00:11 +00:00
const result = await done.promise;
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
2017-07-07 20:02:19 +00:00
/**
* the standard handler for new socket connections
*/
2018-03-15 01:29:40 +00:00
private _handleSocketConnection(socketArg) {
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
});
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
2018-03-15 01:29:40 +00:00
this.openSockets.add(socketConnection);
socketConnection
.authenticate()
2017-07-07 20:02:19 +00:00
.then(() => {
2018-03-15 01:29:40 +00:00
return socketConnection.listenToFunctionRequests();
2017-07-07 20:02:19 +00:00
})
2018-03-15 01:29:40 +00:00
.catch(err => {
console.log(err);
});
2017-07-07 20:02:19 +00:00
}
}