smartsocket/ts/smartsocket.classes.smartsocket.ts

60 lines
1.8 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";
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({
authenticated:false,
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(socketConnection.listenToFunctionRequests);
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);
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(){
// TODO: target specific client and initiate response
2016-08-07 12:58:20 +00:00
}
}