smartsocket/dist/smartsocket.classes.smartsocketclient.js

71 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-08-07 16:59:39 +00:00
"use strict";
2017-07-07 20:02:19 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2016-08-09 09:42:21 +00:00
const plugins = require("./smartsocket.plugins");
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
2016-08-09 21:37:25 +00:00
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
2016-08-07 16:59:39 +00:00
class SmartsocketClient {
2016-08-09 21:37:25 +00:00
constructor(optionsArg) {
2016-08-14 01:25:26 +00:00
this.alias = optionsArg.alias;
this.role = optionsArg.role;
this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password;
}
2016-08-14 01:25:26 +00:00
/**
* connect the client to the server
*/
connect() {
2017-07-07 20:02:19 +00:00
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({
2016-08-14 01:25:26 +00:00
alias: this.alias,
authenticated: false,
2016-08-14 01:25:26 +00:00
role: undefined,
2017-07-07 20:02:19 +00:00
side: 'client',
2016-09-04 22:34:09 +00:00
smartsocketHost: null,
2016-08-14 20:17:55 +00:00
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
});
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.on('requestAuth', () => {
console.log('server requested authentication');
this.socketConnection.socket.emit('dataAuth', {
2016-08-14 01:25:26 +00:00
role: this.role,
password: this.serverPassword,
alias: this.alias
});
2017-07-07 20:02:19 +00:00
this.socketConnection.socket.on('authenticated', () => {
console.log('client is authenticated');
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
});
return done.promise;
2016-08-09 09:42:21 +00:00
}
2016-08-14 01:25:26 +00:00
disconnect() {
2017-07-07 20:02:19 +00:00
let done = plugins.smartq.defer();
2016-08-14 01:25:26 +00:00
this.socketConnection.socket.disconnect();
2016-08-14 20:17:55 +00:00
this.socketConnection = undefined;
2017-07-07 20:02:19 +00:00
plugins.beautylog.ok('disconnected!');
2016-08-14 01:25:26 +00:00
done.resolve();
return done.promise;
}
2016-08-09 21:37:25 +00:00
serverCall(functionNameArg, dataArg) {
2017-07-07 20:02:19 +00:00
let done = plugins.smartq.defer();
2016-08-09 21:37:25 +00:00
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
2017-07-07 20:02:19 +00:00
side: 'requesting',
2016-08-09 21:37:25 +00:00
originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
});
2018-03-15 01:29:40 +00:00
socketRequest.dispatch().then((dataArg) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
2016-08-09 21:37:25 +00:00
}
2016-08-07 16:59:39 +00:00
}
exports.SmartsocketClient = SmartsocketClient;
2018-03-19 09:00:11 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBaUQ7QUFFakQsaUdBQTBFO0FBRTFFLDJGQUE4RjtBQWE5RjtJQU9FLFlBQVksVUFBcUM7UUFDL0MsSUFBSSxDQUFDLEtBQUssR0FBRyxVQUFVLENBQUMsS0FBSyxDQUFDO1FBQzlCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsU0FBUyxHQUFHLFVBQVUsQ0FBQyxHQUFHLENBQUM7UUFDaEMsSU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