smartsocket/ts/smartsocket.classes.smartsocket.ts

98 lines
3.0 KiB
TypeScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// classes
import { Objectmap } from "lik";
import { SocketFunction,ISocketFunctionCall} from "./smartsocket.classes.socketfunction";
2016-08-08 16:20:00 +00:00
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketRequest } from "./smartsocket.classes.socketrequest";
import { SocketRole } from "./smartsocket.classes.socketrole";
2016-08-07 12:58:20 +00:00
export interface ISmartsocketConstructorOptions {
port: number;
2016-08-07 13:37:52 +00:00
};
2016-08-07 12:58:20 +00:00
export class Smartsocket {
2016-08-08 16:20:00 +00:00
options: ISmartsocketConstructorOptions
2016-08-07 12:58:20 +00:00
io: SocketIO.Server;
2016-08-08 16:20:00 +00:00
openSockets = new Objectmap<SocketConnection>();
2016-09-04 22:34:09 +00:00
socketRoles = new Objectmap<SocketRole>();
2016-08-07 16:59:39 +00:00
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
2016-08-07 12:58:20 +00:00
};
/**
* the standard handler for new socket connections
*/
2016-08-11 23:32:57 +00:00
private _handleSocketConnection(socketArg) {
2016-08-08 16:20:00 +00:00
let socketConnection: SocketConnection = new SocketConnection({
2016-08-14 01:25:26 +00:00
alias:undefined,
2016-08-08 16:20:00 +00:00
authenticated:false,
2016-08-14 01:25:26 +00:00
role:undefined,
side:"server",
2016-09-04 22:34:09 +00:00
smartsocketHost: this,
2016-08-08 16:20:00 +00:00
socket:socketArg
});
2016-08-07 16:59:39 +00:00
plugins.beautylog.log("Socket connected. Trying to authenticate...")
2016-08-08 16:20:00 +00:00
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch((err) => {
console.log(err);
});
2016-08-07 13:37:52 +00:00
};
2016-08-07 16:59:39 +00:00
/**
* starts listening to incling sockets:
*/
startServer = () => {
this.io = plugins.socketIo(this.options.port);
2016-08-14 01:25:26 +00:00
this.io.on("connection", (socketArg) => {
2016-08-11 23:32:57 +00:00
this._handleSocketConnection(socketArg);
2016-08-07 16:59:39 +00:00
});
}
2016-08-07 12:58:20 +00:00
closeServer = () => {
2016-08-08 16:20:00 +00:00
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
2016-08-07 16:59:39 +00:00
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
2016-08-07 12:58:20 +00:00
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
2016-08-07 16:59:39 +00:00
this.io.close();
2016-08-11 23:32:57 +00:00
};
// communication
/**
* allows call to specific client.
*/
clientCall(functionNameArg:string,dataArg:any,targetSocketConnectionArg:SocketConnection){
let done = plugins.q.defer();
let socketRequest = new SocketRequest({
side:"requesting",
originSocketConnection:targetSocketConnectionArg,
shortId:plugins.shortid.generate(),
funcCallData:{
funcName: functionNameArg,
funcDataArg:dataArg
}
});
socketRequest.dispatch()
.then((dataArg:ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
2016-09-04 22:34:09 +00:00
};
/**
* adds socketRoles
*/
addSocketRoles(socketRolesArray:SocketRole[]):void{
for(let socketRole of socketRolesArray){
this.socketRoles.add(socketRole);
};
return;
};
2016-08-07 12:58:20 +00:00
}