smartsocket/ts/smartsocket.classes.smartsocketclient.ts

101 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-03-15 01:29:40 +00:00
import * as plugins from './smartsocket.plugins';
2016-08-09 09:42:21 +00:00
2018-03-15 01:29:40 +00:00
import { SocketConnection } from './smartsocket.classes.socketconnection';
2018-03-19 09:00:11 +00:00
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
2019-08-12 20:31:40 +00:00
import { SocketRole } from './smartsocket.classes.socketrole';
2018-03-19 09:00:11 +00:00
2016-08-07 12:58:20 +00:00
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
2018-03-15 01:29:40 +00:00
port: number;
url: string;
alias: string; // an alias makes it easier to identify this client in a multo client environment
role: string;
password: string; // by setting a password access to functions can be limited
2016-08-07 12:58:20 +00:00
}
export class SmartsocketClient {
2019-08-12 20:31:40 +00:00
public alias: string;
public role: string;
public socketConnection: SocketConnection;
public serverUrl: string;
public serverPort: number;
public serverPassword: string;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
public socketRoles = new plugins.lik.Objectmap<SocketRole>();
2017-07-07 20:02:19 +00:00
constructor(optionsArg: ISmartsocketClientOptions) {
2018-03-15 01:29:40 +00:00
this.alias = optionsArg.alias;
this.role = optionsArg.role;
this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password;
2017-07-07 20:02:19 +00:00
}
2016-08-11 23:32:57 +00:00
2019-08-12 20:31:40 +00:00
public addSocketFunction(socketFunction: SocketFunction) {
this.socketFunctions.add(socketFunction);
}
2017-07-07 20:02:19 +00:00
/**
* connect the client to the server
*/
2019-08-12 20:31:40 +00:00
public connect() {
const done = plugins.smartpromise.defer();
plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
2019-08-12 20:31:40 +00:00
const socketUrl = `${this.serverUrl}:${this.serverPort}`;
2017-07-07 20:02:19 +00:00
this.socketConnection = new SocketConnection({
alias: this.alias,
authenticated: false,
role: undefined,
side: 'client',
2019-08-12 20:31:40 +00:00
smartsocketHost: this,
2017-07-07 20:02:19 +00:00
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.on('requestAuth', () => {
2018-03-15 01:29:40 +00:00
console.log('server requested authentication');
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.emit('dataAuth', {
role: this.role,
password: this.serverPassword,
alias: this.alias
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.on('authenticated', () => {
2018-03-15 01:29:40 +00:00
console.log('client is authenticated');
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
});
return done.promise;
2017-07-07 20:02:19 +00:00
}
2017-10-09 08:28:18 +00:00
2019-08-12 20:31:40 +00:00
public disconnect() {
const done = plugins.smartpromise.defer();
2018-03-15 01:29:40 +00:00
this.socketConnection.socket.disconnect();
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
2018-03-15 01:29:40 +00:00
done.resolve();
return done.promise;
2017-07-07 20:02:19 +00:00
}
2018-03-15 01:29:40 +00:00
2019-08-12 20:31:40 +00:00
public serverCall(functionNameArg: string, dataArg: any) {
const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, {
2017-07-07 20:02:19 +00:00
side: 'requesting',
originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
2018-03-15 01:29:40 +00:00
});
2019-08-12 20:31:40 +00:00
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => {
done.resolve(dataArg2.funcDataArg);
2018-03-15 01:29:40 +00:00
});
return done.promise;
2017-07-07 20:02:19 +00:00
}
}