smartsocket/dist/smartsocket.classes.socketconnection.js

85 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-08-08 16:20:00 +00:00
"use strict";
2016-08-09 09:42:21 +00:00
const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
2016-08-08 16:20:00 +00:00
;
2016-08-09 09:42:21 +00:00
// export classes
/**
* class SocketConnection represents a websocket connection
*/
2016-08-08 16:20:00 +00:00
class SocketConnection {
constructor(optionsArg) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
// authenticating --------------------------
2016-08-09 09:42:21 +00:00
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
if ((true)) {
this.alias = dataArg.alias;
this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role);
2016-08-09 09:42:21 +00:00
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
}
else {
this.socket.disconnect();
done.reject("not authenticated");
}
;
});
this.socket.emit("requestAuth");
return done.promise;
}
;
// listening -------------------------------
2016-08-09 09:42:21 +00:00
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.q.defer();
if (this.authenticated) {
this.socket.on("function", (dataArg) => {
2016-08-09 21:37:25 +00:00
// check if requested function is available to the socket's scope
2016-08-09 09:42:21 +00:00
let referencedFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
2016-08-09 21:37:25 +00:00
return socketFunctionArg.name === dataArg.funcCallData.funcName;
2016-08-09 09:42:21 +00:00
});
if (referencedFunction !== undefined) {
let localSocketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
side: "responding",
2016-08-09 21:37:25 +00:00
originSocketConnection: this,
shortId: dataArg.shortId,
funcCallData: dataArg.funcCallData
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
2016-08-09 09:42:21 +00:00
}
else {
plugins.beautylog.warn("function not existent or out of access scope");
}
;
});
this.socket.on("functionResponse", (dataArg) => {
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg);
});
2016-08-09 09:42:21 +00:00
}
else {
done.reject("socket needs to be authenticated first");
}
;
return done.promise;
}
;
2016-08-08 16:20:00 +00:00
}
exports.SocketConnection = SocketConnection;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSx1QkFBdUIsQ0FBQyxDQUFBO0FBQ2pELE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFJakQsb0RBQTJFLHFDQUFxQyxDQUFDLENBQUE7QUFhaEgsQ0FBQztBQVdGLGlCQUFpQjtBQUVqQjs7R0FFRztBQUNIO0lBS0ksWUFBWSxVQUErQztRQUN2RCxJQUFJLENBQUMsS0FBSyxHQUFHLFVBQVUsQ0FBQyxLQUFLLENBQUM7UUFDOUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxVQUFVLENBQUMsYUFBYSxDQUFDO1FBQzlDLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsTUFBTSxHQUFHLFVBQVUsQ0FBQyxNQUFNLENBQUM7SUFDcEMsQ0FBQztJQUVELDRDQUE0QztJQUU1Qzs7T0FFRztJQUNILFlBQVk7UUFDUixJQUFJLElB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