smartsocket/dist/smartsocket.classes.smartsocketclient.js

73 lines
5.8 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3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