smartsocket/ts/smartsocket.classes.smartsocket.ts

124 lines
3.3 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
2018-03-15 01:29:40 +00:00
import * as http from 'http';
2017-10-09 08:40:59 +00:00
2016-08-07 12:58:20 +00:00
// classes
2018-03-15 01:29:40 +00:00
import { Objectmap } from 'lik';
import { SocketFunction, ISocketFunctionCall } from './smartsocket.classes.socketfunction';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
// 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-15 01:29:40 +00:00
options: ISmartsocketConstructorOptions;
httpServer: http.Server;
io: SocketIO.Server;
openSockets = new Objectmap<SocketConnection>();
socketRoles = new Objectmap<SocketRole>();
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
2017-07-07 20:02:19 +00:00
}
2016-08-07 12:58:20 +00:00
2017-07-07 20:02:19 +00:00
/**
* starts listening to incoming sockets:
2017-07-07 20:02:19 +00:00
*/
2018-03-15 01:29:40 +00:00
async startServer() {
let done = plugins.smartq.defer();
2017-10-09 08:40:59 +00:00
if (!this.httpServer) {
2018-03-15 01:29:40 +00:00
this.httpServer = new http.Server();
2017-10-09 08:40:59 +00:00
}
2018-03-15 01:29:40 +00:00
this.io = plugins.socketIo(this.httpServer);
this.io.on('connection', socketArg => {
this._handleSocketConnection(socketArg);
});
2017-10-09 08:40:59 +00:00
this.httpServer.listen(this.options.port, () => {
2018-03-15 01:29:40 +00:00
done.resolve();
});
return await done.promise;
2017-07-07 20:02:19 +00:00
}
2017-10-09 01:06:09 +00:00
/**
* starts the server with another server
*/
2017-10-09 08:40:59 +00:00
async setServer(httpServerArg: http.Server) {
2018-03-15 01:29:40 +00:00
this.httpServer = httpServerArg;
2017-10-09 01:06:09 +00:00
}
/**
* closes the server
*/
2018-03-15 01:29:40 +00:00
async closeServer() {
await plugins.smartdelay.delayFor(1000);
2017-07-07 20:02:19 +00:00
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
2018-03-15 01:29:40 +00:00
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
this.io.close();
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-15 01:29:40 +00:00
clientCall(functionNameArg: string, dataArg: any, targetSocketConnectionArg: SocketConnection) {
let done = plugins.smartq.defer();
2017-07-07 20:02:19 +00:00
let socketRequest = new SocketRequest({
side: 'requesting',
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
2018-03-15 01:29:40 +00:00
});
socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
2017-07-07 20:02:19 +00:00
}
2017-07-07 20:02:19 +00:00
/**
* adds socketRoles
*/
2018-03-15 01:29:40 +00:00
addSocketRoles(socketRolesArray: SocketRole[]): void {
2017-07-07 20:02:19 +00:00
for (let 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) {
2017-07-07 20:02:19 +00:00
let socketConnection: SocketConnection = new SocketConnection({
alias: undefined,
authenticated: false,
role: undefined,
side: 'server',
smartsocketHost: this,
socket: socketArg
2018-03-15 01:29:40 +00:00
});
plugins.beautylog.log('Socket connected. Trying to authenticate...');
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
}
}