update to latest standards
This commit is contained in:
@@ -1,65 +1,68 @@
|
||||
import * as plugins from './smartsocket.plugins'
|
||||
import * as helpers from './smartsocket.helpers'
|
||||
import * as plugins from './smartsocket.plugins';
|
||||
import * as helpers from './smartsocket.helpers';
|
||||
|
||||
import * as http from 'http'
|
||||
import * as http from 'http';
|
||||
|
||||
// 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';
|
||||
|
||||
// socket.io
|
||||
import * as SocketIO from 'socket.io';
|
||||
|
||||
export interface ISmartsocketConstructorOptions {
|
||||
port: number
|
||||
port: number;
|
||||
}
|
||||
|
||||
export class Smartsocket {
|
||||
options: ISmartsocketConstructorOptions
|
||||
httpServer: http.Server
|
||||
io: SocketIO.Server
|
||||
openSockets = new Objectmap<SocketConnection>()
|
||||
socketRoles = new Objectmap<SocketRole>()
|
||||
constructor (optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg
|
||||
options: ISmartsocketConstructorOptions;
|
||||
httpServer: http.Server;
|
||||
io: SocketIO.Server;
|
||||
openSockets = new Objectmap<SocketConnection>();
|
||||
socketRoles = new Objectmap<SocketRole>();
|
||||
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts listening to incoming sockets:
|
||||
*/
|
||||
async startServer () {
|
||||
let done = plugins.smartq.defer()
|
||||
async startServer() {
|
||||
let done = plugins.smartq.defer();
|
||||
if (!this.httpServer) {
|
||||
this.httpServer = new http.Server()
|
||||
this.httpServer = new http.Server();
|
||||
}
|
||||
this.io = plugins.socketIo(this.httpServer)
|
||||
this.io.on('connection', (socketArg) => {
|
||||
this._handleSocketConnection(socketArg)
|
||||
})
|
||||
this.io = plugins.socketIo(this.httpServer);
|
||||
this.io.on('connection', socketArg => {
|
||||
this._handleSocketConnection(socketArg);
|
||||
});
|
||||
this.httpServer.listen(this.options.port, () => {
|
||||
done.resolve()
|
||||
})
|
||||
return await done.promise
|
||||
done.resolve();
|
||||
});
|
||||
return await done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts the server with another server
|
||||
*/
|
||||
async setServer(httpServerArg: http.Server) {
|
||||
this.httpServer = httpServerArg
|
||||
this.httpServer = httpServerArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* closes the server
|
||||
*/
|
||||
async closeServer () {
|
||||
await plugins.smartdelay.delayFor(1000)
|
||||
async closeServer() {
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
|
||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`)
|
||||
socketObjectArg.socket.disconnect()
|
||||
})
|
||||
this.openSockets.wipe()
|
||||
this.io.close()
|
||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
||||
socketObjectArg.socket.disconnect();
|
||||
});
|
||||
this.openSockets.wipe();
|
||||
this.io.close();
|
||||
}
|
||||
|
||||
// communication
|
||||
@@ -67,8 +70,8 @@ export class Smartsocket {
|
||||
/**
|
||||
* allows call to specific client.
|
||||
*/
|
||||
clientCall (functionNameArg: string, dataArg: any, targetSocketConnectionArg: SocketConnection) {
|
||||
let done = plugins.smartq.defer()
|
||||
clientCall(functionNameArg: string, dataArg: any, targetSocketConnectionArg: SocketConnection) {
|
||||
let done = plugins.smartq.defer();
|
||||
let socketRequest = new SocketRequest({
|
||||
side: 'requesting',
|
||||
originSocketConnection: targetSocketConnectionArg,
|
||||
@@ -77,28 +80,27 @@ export class Smartsocket {
|
||||
funcName: functionNameArg,
|
||||
funcDataArg: dataArg
|
||||
}
|
||||
})
|
||||
socketRequest.dispatch()
|
||||
.then((dataArg: ISocketFunctionCall) => {
|
||||
done.resolve(dataArg.funcDataArg)
|
||||
})
|
||||
return done.promise
|
||||
});
|
||||
socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
|
||||
done.resolve(dataArg.funcDataArg);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds socketRoles
|
||||
*/
|
||||
addSocketRoles (socketRolesArray: SocketRole[]): void {
|
||||
addSocketRoles(socketRolesArray: SocketRole[]): void {
|
||||
for (let socketRole of socketRolesArray) {
|
||||
this.socketRoles.add(socketRole)
|
||||
this.socketRoles.add(socketRole);
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* the standard handler for new socket connections
|
||||
*/
|
||||
private _handleSocketConnection (socketArg) {
|
||||
private _handleSocketConnection(socketArg) {
|
||||
let socketConnection: SocketConnection = new SocketConnection({
|
||||
alias: undefined,
|
||||
authenticated: false,
|
||||
@@ -106,16 +108,16 @@ export class Smartsocket {
|
||||
side: 'server',
|
||||
smartsocketHost: this,
|
||||
socket: socketArg
|
||||
})
|
||||
plugins.beautylog.log('Socket connected. Trying to authenticate...')
|
||||
this.openSockets.add(socketConnection)
|
||||
socketConnection.authenticate()
|
||||
});
|
||||
plugins.beautylog.log('Socket connected. Trying to authenticate...');
|
||||
this.openSockets.add(socketConnection);
|
||||
socketConnection
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
return socketConnection.listenToFunctionRequests()
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
return socketConnection.listenToFunctionRequests();
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user