2016-08-07 16:59:39 +00:00
|
|
|
"use strict";
|
2016-08-09 09:42:21 +00:00
|
|
|
const plugins = require("./smartsocket.plugins");
|
2016-08-12 03:56:40 +00:00
|
|
|
// 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;
|
2016-08-12 03:56:40 +00:00
|
|
|
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() {
|
2016-08-12 03:56:40 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-14 01:25:26 +00:00
|
|
|
plugins.beautylog.log("trying to connect...");
|
2016-08-12 03:56:40 +00:00
|
|
|
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,
|
2016-08-12 03:56:40 +00:00
|
|
|
authenticated: false,
|
2016-08-14 01:25:26 +00:00
|
|
|
role: undefined,
|
2016-08-14 20:17:55 +00:00
|
|
|
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
|
2016-08-12 03:56:40 +00:00
|
|
|
});
|
2016-08-14 01:25:26 +00:00
|
|
|
this.socketConnection.socket.on("requestAuth", () => {
|
2016-08-12 03:56:40 +00:00
|
|
|
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
|
2016-08-12 03:56:40 +00:00
|
|
|
});
|
|
|
|
this.socketConnection.socket.on("authenticated", () => {
|
|
|
|
console.log("client is authenticated");
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
|
|
|
;
|
2016-08-14 01:25:26 +00:00
|
|
|
disconnect() {
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
this.socketConnection.socket.disconnect();
|
2016-08-14 20:17:55 +00:00
|
|
|
this.socketConnection = undefined;
|
2016-08-14 01:25:26 +00:00
|
|
|
plugins.beautylog.ok("disconnected!");
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
}
|
2016-08-09 21:37:25 +00:00
|
|
|
serverCall(functionNameArg, dataArg) {
|
2016-08-12 03:56:40 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-09 21:37:25 +00:00
|
|
|
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
|
|
|
|
side: "requesting",
|
|
|
|
originSocketConnection: this.socketConnection,
|
|
|
|
shortId: plugins.shortid.generate(),
|
|
|
|
funcCallData: {
|
|
|
|
funcName: functionNameArg,
|
|
|
|
funcDataArg: dataArg
|
|
|
|
}
|
|
|
|
});
|
2016-08-12 03:56:40 +00:00
|
|
|
socketRequest.dispatch()
|
|
|
|
.then(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-08-09 21:37:25 +00:00
|
|
|
}
|
2016-08-12 03:56:40 +00:00
|
|
|
;
|
2016-08-07 16:59:39 +00:00
|
|
|
}
|
|
|
|
exports.SmartsocketClient = SmartsocketClient;
|
2016-08-14 20:17:55 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5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