smartsocket/ts/smartsocket.classes.smartsocket.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

2016-08-07 14:58:20 +02:00
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// classes
import { Objectmap } from "lik";
2016-08-08 18:20:00 +02:00
import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
2016-08-07 14:58:20 +02:00
export interface ISmartsocketConstructorOptions {
port: number;
2016-08-07 15:37:52 +02:00
};
2016-08-07 14:58:20 +02:00
export class Smartsocket {
2016-08-08 18:20:00 +02:00
options: ISmartsocketConstructorOptions
2016-08-07 14:58:20 +02:00
io: SocketIO.Server;
2016-08-08 18:20:00 +02:00
openSockets = new Objectmap<SocketConnection>();
2016-08-07 18:59:39 +02:00
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
2016-08-07 14:58:20 +02:00
};
/**
* the standard handler for new socket connections
*/
2016-08-12 01:32:57 +02:00
private _handleSocketConnection(socketArg) {
2016-08-08 18:20:00 +02:00
let socketConnection: SocketConnection = new SocketConnection({
authenticated:false,
socket:socketArg
});
2016-08-07 18:59:39 +02:00
plugins.beautylog.log("Socket connected. Trying to authenticate...")
2016-08-08 18:20:00 +02:00
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(socketConnection.listenToFunctionRequests);
2016-08-07 15:37:52 +02:00
};
2016-08-07 18:59:39 +02:00
/**
* starts listening to incling sockets:
*/
2016-08-07 14:58:20 +02:00
2016-08-07 18:59:39 +02:00
startServer = () => {
this.io = plugins.socketIo(this.options.port);
this.io.on('connection', (socketArg) => {
2016-08-12 01:32:57 +02:00
this._handleSocketConnection(socketArg);
2016-08-07 18:59:39 +02:00
});
}
2016-08-07 14:58:20 +02:00
closeServer = () => {
2016-08-08 18:20:00 +02:00
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
2016-08-07 18:59:39 +02:00
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
2016-08-07 14:58:20 +02:00
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
2016-08-07 18:59:39 +02:00
this.io.close();
2016-08-12 01:32:57 +02:00
};
// communication
clientCall(){
// TODO: target specific client and initiate response
2016-08-07 14:58:20 +02:00
}
}