smartsocket/ts/smartsocket.classes.smartsocket.ts

122 lines
3.2 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
2017-10-09 08:40:59 +00:00
import * as http from 'http'
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
2017-10-09 08:40:59 +00:00
httpServer: http.Server
2017-07-07 20:02:19 +00:00
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 incoming sockets:
2017-07-07 20:02:19 +00:00
*/
async startServer () {
2017-10-09 08:40:59 +00:00
let done = plugins.smartq.defer()
if (!this.httpServer) {
this.httpServer = new http.Server()
}
this.io = plugins.socketIo(this.httpServer)
2017-07-07 20:02:19 +00:00
this.io.on('connection', (socketArg) => {
this._handleSocketConnection(socketArg)
})
2017-10-09 08:40:59 +00:00
this.httpServer.listen(this.options.port, () => {
done.resolve()
})
return await done.promise
2017-07-07 20:02:19 +00:00
}
2017-10-09 01:06:09 +00:00
/**
* starts the server with another server
*/
2017-10-09 08:40:59 +00:00
async setServer(httpServerArg: http.Server) {
this.httpServer = httpServerArg
2017-10-09 01:06:09 +00:00
}
/**
* closes the server
*/
async closeServer () {
2017-10-09 01:06:09 +00:00
await plugins.smartdelay.delayFor(1000)
2017-07-07 20:02:19 +00:00
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
}