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,
|
|
|
|
socket: plugins.socketIoClient(socketUrl, {})
|
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();
|
|
|
|
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 01:25:26 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29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