now tests run trough
This commit is contained in:
@ -8,9 +8,10 @@ import {SocketFunction} from "./smartsocket.classes.socketfunction";
|
||||
|
||||
|
||||
export interface ISocketObject {
|
||||
group?:string,
|
||||
socket: SocketIO.Socket,
|
||||
alias?:string;
|
||||
authenticated: boolean
|
||||
role?:string,
|
||||
socket: SocketIO.Socket,
|
||||
};
|
||||
|
||||
export interface ISmartsocketConstructorOptions {
|
||||
@ -19,12 +20,12 @@ export interface ISmartsocketConstructorOptions {
|
||||
};
|
||||
|
||||
export class Smartsocket {
|
||||
options:ISmartsocketConstructorOptions
|
||||
io: SocketIO.Server;
|
||||
openSockets = new Objectmap();
|
||||
registeredRoles = new Objectmap();
|
||||
constructor(options: ISmartsocketConstructorOptions) {
|
||||
this.io = plugins.socketIo(options.port);
|
||||
this.io.on('connection', this._handleSocket);
|
||||
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -35,22 +36,33 @@ export class Smartsocket {
|
||||
socket: socket,
|
||||
authenticated: false
|
||||
};
|
||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
||||
this.openSockets.add(socketObject);
|
||||
helpers.authenticateSocket(socketObject)
|
||||
.then();
|
||||
}
|
||||
|
||||
registerRole(socketRoleArg:SocketRole){
|
||||
registerFunctions(socketRoleArg:SocketRole){
|
||||
this.registeredRoles.add(socketRoleArg);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* starts listening to incling sockets:
|
||||
*/
|
||||
|
||||
startServer = () => {
|
||||
this.io = plugins.socketIo(this.options.port);
|
||||
this.io.on('connection', (socketArg) => {
|
||||
this._handleSocket(socketArg);
|
||||
});
|
||||
}
|
||||
closeServer = () => {
|
||||
this.io.close();
|
||||
this.openSockets.forEach((socketObjectArg: ISocketObject) => {
|
||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
||||
socketObjectArg.socket.disconnect();
|
||||
});
|
||||
this.openSockets.wipe();
|
||||
this.io.close();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import * as plugins from "./smartsocket.plugins";
|
||||
|
||||
// import classes
|
||||
import { Stringmap } from "lik";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
import * as plugins from "./smartsocket.plugins";
|
||||
|
||||
// import classes
|
||||
import { Stringmap } from "lik";
|
||||
|
||||
/**
|
||||
* interface for class SocketRole
|
||||
*/
|
||||
|
@ -11,9 +11,20 @@ import {ISocketObject} from "./smartsocket.classes.smartsocket";
|
||||
*/
|
||||
export let authenticateSocket = (socketObjectArg: ISocketObject) => {
|
||||
let done = plugins.q.defer();
|
||||
socketObjectArg.socket.on("dataAuth", data => {
|
||||
socketObjectArg.socket.on("dataAuth", dataArg => {
|
||||
plugins.beautylog.log("received authentication data. now hashing and comparing...");
|
||||
socketObjectArg.socket.removeListener("dataAuth", () => { });
|
||||
done.resolve();
|
||||
if((true)){ // TODO: authenticate password
|
||||
socketObjectArg.alias = dataArg.alias
|
||||
socketObjectArg.authenticated = true;
|
||||
socketObjectArg.role = dataArg.role;
|
||||
socketObjectArg.socket.emit("authenticated");
|
||||
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`)
|
||||
done.resolve(socketObjectArg);
|
||||
} else {
|
||||
socketObjectArg.socket.disconnect();
|
||||
done.reject("not authenticated");
|
||||
};
|
||||
});
|
||||
socketObjectArg.socket.emit("requestAuth");
|
||||
return done.promise;
|
||||
|
Reference in New Issue
Block a user