update to latest standards
This commit is contained in:
@@ -1,98 +1,98 @@
|
||||
import * as plugins from "./smartsocket.plugins";
|
||||
import * as helpers from "./smartsocket.helpers";
|
||||
import * as plugins from './smartsocket.plugins'
|
||||
import * as helpers from './smartsocket.helpers'
|
||||
|
||||
// classes
|
||||
import { Objectmap } from "lik";
|
||||
import { SocketFunction,ISocketFunctionCall} from "./smartsocket.classes.socketfunction";
|
||||
import { SocketConnection } from "./smartsocket.classes.socketconnection";
|
||||
import { SocketRequest } from "./smartsocket.classes.socketrequest";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
import { Objectmap } from 'lik'
|
||||
import { SocketFunction, ISocketFunctionCall } from './smartsocket.classes.socketfunction'
|
||||
import { SocketConnection } from './smartsocket.classes.socketconnection'
|
||||
import { SocketRequest } from './smartsocket.classes.socketrequest'
|
||||
import { SocketRole } from './smartsocket.classes.socketrole'
|
||||
|
||||
export interface ISmartsocketConstructorOptions {
|
||||
port: number;
|
||||
};
|
||||
port: number
|
||||
}
|
||||
|
||||
export class Smartsocket {
|
||||
options: ISmartsocketConstructorOptions
|
||||
io: SocketIO.Server;
|
||||
openSockets = new Objectmap<SocketConnection>();
|
||||
socketRoles = new Objectmap<SocketRole>();
|
||||
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
};
|
||||
options: ISmartsocketConstructorOptions
|
||||
io: SocketIO.Server
|
||||
openSockets = new Objectmap<SocketConnection>()
|
||||
socketRoles = new Objectmap<SocketRole>()
|
||||
constructor (optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg
|
||||
}
|
||||
|
||||
/**
|
||||
* the standard handler for new socket connections
|
||||
*/
|
||||
private _handleSocketConnection(socketArg) {
|
||||
let socketConnection: SocketConnection = new SocketConnection({
|
||||
alias:undefined,
|
||||
authenticated:false,
|
||||
role:undefined,
|
||||
side:"server",
|
||||
smartsocketHost: this,
|
||||
socket:socketArg
|
||||
});
|
||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
||||
this.openSockets.add(socketConnection);
|
||||
socketConnection.authenticate()
|
||||
.then(() => {
|
||||
return socketConnection.listenToFunctionRequests();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
};
|
||||
/**
|
||||
* starts listening to incling sockets:
|
||||
*/
|
||||
startServer = () => {
|
||||
this.io = plugins.socketIo(this.options.port)
|
||||
this.io.on('connection', (socketArg) => {
|
||||
this._handleSocketConnection(socketArg)
|
||||
})
|
||||
}
|
||||
closeServer = () => {
|
||||
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`)
|
||||
socketObjectArg.socket.disconnect()
|
||||
})
|
||||
this.openSockets.wipe()
|
||||
this.io.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* starts listening to incling sockets:
|
||||
*/
|
||||
startServer = () => {
|
||||
this.io = plugins.socketIo(this.options.port);
|
||||
this.io.on("connection", (socketArg) => {
|
||||
this._handleSocketConnection(socketArg);
|
||||
});
|
||||
// communication
|
||||
|
||||
/**
|
||||
* allows call to specific client.
|
||||
*/
|
||||
clientCall (functionNameArg: string, dataArg: any, targetSocketConnectionArg: SocketConnection) {
|
||||
let done = plugins.smartq.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
|
||||
}
|
||||
|
||||
/**
|
||||
* adds socketRoles
|
||||
*/
|
||||
addSocketRoles (socketRolesArray: SocketRole[]): void {
|
||||
for (let socketRole of socketRolesArray) {
|
||||
this.socketRoles.add(socketRole)
|
||||
}
|
||||
closeServer = () => {
|
||||
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
||||
socketObjectArg.socket.disconnect();
|
||||
});
|
||||
this.openSockets.wipe();
|
||||
this.io.close();
|
||||
};
|
||||
return
|
||||
}
|
||||
|
||||
// communication
|
||||
/**
|
||||
* the standard handler for new socket connections
|
||||
*/
|
||||
private _handleSocketConnection (socketArg) {
|
||||
let socketConnection: SocketConnection = new SocketConnection({
|
||||
alias: undefined,
|
||||
authenticated: false,
|
||||
role: undefined,
|
||||
side: 'server',
|
||||
smartsocketHost: this,
|
||||
socket: socketArg
|
||||
})
|
||||
plugins.beautylog.log('Socket connected. Trying to authenticate...')
|
||||
this.openSockets.add(socketConnection)
|
||||
socketConnection.authenticate()
|
||||
.then(() => {
|
||||
return socketConnection.listenToFunctionRequests()
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* adds socketRoles
|
||||
*/
|
||||
addSocketRoles(socketRolesArray:SocketRole[]):void{
|
||||
for(let socketRole of socketRolesArray){
|
||||
this.socketRoles.add(socketRole);
|
||||
};
|
||||
return;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user