smartsocket/dist/smartsocket.classes.smartsocketclient.js

72 lines
5.8 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");
// import classes
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-15 01:29:40 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBaUQ7QUFNakQsaUJBQWlCO0FBQ2pCLGlHQUEwRTtBQUUxRSwyRkFBb0U7QUFZcEU7SUFPRSxZQUFZLFVBQXFDO1FBQy9DLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQztRQUM5QixJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFNBQVMsR0FBRyxVQUFVLENBQ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