smartsocket/dist/smartsocket.classes.smartsocketclient.js

75 lines
5.8 KiB
JavaScript

"use strict";
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.q.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.q.defer();
this.socketConnection.socket.disconnect();
this.socketConnection = undefined;
plugins.beautylog.ok("disconnected!");
done.resolve();
return done.promise;
}
serverCall(functionNameArg, dataArg) {
let done = plugins.q.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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC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