smartsocket/dist/smartsocket.classes.smartsocketclient.js

73 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
}
});
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;
2017-10-09 01:06:09 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBZ0Q7QUFPaEQsaUJBQWlCO0FBQ2pCLGlHQUF5RTtBQUV6RSwyRkFBbUU7QUFZbkU7SUFPRSxZQUFZLFVBQXFDO1FBQy9DLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQTtRQUM3QixJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUE7UUFDM0IsSUFBSSxDQUFDLFNBQVMsR0FBRyxVQUFVLENBQUMsR0FBRyxDQU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