smartsocket/ts/smartsocket.classes.smartsocket.ts

99 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-07-07 20:02:19 +00:00
import * as plugins from './smartsocket.plugins'
import * as helpers from './smartsocket.helpers'
2016-08-07 12:58:20 +00:00
// classes
2017-07-07 20:02:19 +00:00
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'
2016-08-07 12:58:20 +00:00
export interface ISmartsocketConstructorOptions {
2017-07-07 20:02:19 +00:00
port: number
}
2016-08-07 12:58:20 +00:00
export class Smartsocket {
2017-07-07 20:02:19 +00:00
options: ISmartsocketConstructorOptions
io: SocketIO.Server
openSockets = new Objectmap<SocketConnection>()
socketRoles = new Objectmap<SocketRole>()
constructor (optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg
}
2016-08-07 12:58:20 +00:00
2017-07-07 20:02:19 +00:00
/**
* 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()
}
2016-08-07 13:37:52 +00:00
2017-07-07 20:02:19 +00:00
// communication
2016-08-11 23:32:57 +00:00
2017-07-07 20:02:19 +00:00
/**
* 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
}
2017-07-07 20:02:19 +00:00
/**
* adds socketRoles
*/
addSocketRoles (socketRolesArray: SocketRole[]): void {
for (let socketRole of socketRolesArray) {
this.socketRoles.add(socketRole)
}
return
}
2016-09-04 22:34:09 +00:00
2017-07-07 20:02:19 +00:00
/**
* 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)
})
}
2016-09-04 22:34:09 +00:00
2017-07-07 20:02:19 +00:00
}