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";
|
2016-08-08 16:20:00 +00:00
|
|
|
import { SocketRole } from "./smartsocket.classes.socketrole";
|
|
|
|
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
|
|
|
import { SocketConnection } from "./smartsocket.classes.socketconnection";
|
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-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,
|
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()
|
2016-08-14 23:38:28 +00:00
|
|
|
.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:
|
|
|
|
*/
|
2016-08-07 12:58:20 +00:00
|
|
|
|
2016-08-07 16:59:39 +00:00
|
|
|
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
|
|
|
|
clientCall(){
|
2016-08-14 20:17:55 +00:00
|
|
|
|
2016-08-11 23:32:57 +00:00
|
|
|
// TODO: target specific client and initiate response
|
2016-08-07 12:58:20 +00:00
|
|
|
}
|
|
|
|
}
|