73 lines
5.7 KiB
JavaScript
73 lines
5.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const plugins = require("./smartsocket.plugins");
|
|
// import classes
|
|
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
|
|
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
|
|
class SmartsocketClient {
|
|
constructor(optionsArg) {
|
|
this.alias = optionsArg.alias;
|
|
this.role = optionsArg.role;
|
|
this.serverUrl = optionsArg.url;
|
|
this.serverPort = optionsArg.port;
|
|
this.serverPassword = optionsArg.password;
|
|
}
|
|
/**
|
|
* connect the client to the server
|
|
*/
|
|
connect() {
|
|
let done = plugins.smartq.defer();
|
|
plugins.beautylog.log('trying to connect...');
|
|
let socketUrl = `${this.serverUrl}:${this.serverPort}`;
|
|
this.socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
|
|
alias: this.alias,
|
|
authenticated: false,
|
|
role: undefined,
|
|
side: 'client',
|
|
smartsocketHost: null,
|
|
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
|
|
});
|
|
this.socketConnection.socket.on('requestAuth', () => {
|
|
console.log('server requested authentication');
|
|
this.socketConnection.socket.emit('dataAuth', {
|
|
role: this.role,
|
|
password: this.serverPassword,
|
|
alias: this.alias
|
|
});
|
|
this.socketConnection.socket.on('authenticated', () => {
|
|
console.log('client is authenticated');
|
|
this.socketConnection.authenticated = true;
|
|
this.socketConnection.listenToFunctionRequests();
|
|
done.resolve();
|
|
});
|
|
});
|
|
return done.promise;
|
|
}
|
|
disconnect() {
|
|
let done = plugins.smartq.defer();
|
|
this.socketConnection.socket.disconnect();
|
|
this.socketConnection = undefined;
|
|
plugins.beautylog.ok('disconnected!');
|
|
done.resolve();
|
|
return done.promise;
|
|
}
|
|
serverCall(functionNameArg, dataArg) {
|
|
let done = plugins.smartq.defer();
|
|
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
|
|
side: 'requesting',
|
|
originSocketConnection: this.socketConnection,
|
|
shortId: plugins.shortid.generate(),
|
|
funcCallData: {
|
|
funcName: functionNameArg,
|
|
funcDataArg: dataArg
|
|
}
|
|
});
|
|
socketRequest.dispatch()
|
|
.then((dataArg) => {
|
|
done.resolve(dataArg.funcDataArg);
|
|
});
|
|
return done.promise;
|
|
}
|
|
}
|
|
exports.SmartsocketClient = SmartsocketClient;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBZ0Q7QUFPaEQsaUJBQWlCO0FBQ2pCLGlHQUF5RTtBQUV6RSwyRkFBbUU7QUFZbkU7SUFPRSxZQUFZLFVBQXFDO1FBQy9DLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQTtRQUM3QixJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUE7UUFDM0IsSUFBSSxDQUFDLFNBQVMsR0FBRyxVQUFVLENBQUMsR0FBRyxDQUFBO1FBQy9CLElBQUksQ0FBQyxVQUFVLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQTtRQUNqQyxJQUFJLENBQUMsY0FBYyxHQUFHLFVBQVUsQ0FBQyxRQUFRLENBQUE7SUFDM0MsQ0FBQztJQUVEOztPQUVHO0lBQ0gsT0FBTztRQUNMLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDakMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsc0JBQXNCLENBQUMsQ0FBQTtR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