smartsocket/dist/smartsocket.classes.socketconnection.js

80 lines
6.0 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;
}
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.findSocketRoleByString(dataArg.role);
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;
}
;
/**
* 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
});
2016-08-09 21:37:25 +00:00
localSocketRequest;
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) => {
});
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;
;
2016-08-09 21:37:25 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSx1QkFBdUIsQ0FBQyxDQUFBO0FBQ2pELE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFJakQsb0RBQXdELHFDQUFxQyxDQUFDLENBQUE7QUFhN0YsQ0FBQztBQVdGLGlCQUFpQjtBQUVqQjs7R0FFRztBQUNIO0lBS0ksWUFBWSxVQUFvQztRQUM1QyxJQUFJLENBQUMsS0FBSyxHQUFHLFVBQVUsQ0FBQyxLQUFLLENBQUM7UUFDOUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxVQUFVLENBQUMsYUFBYSxDQUFDO1FBQzlDLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsTUFBTSxHQUFHLFVBQVUsQ0FBQyxNQUFNLENBQUM7SUFDcEMsQ0FBQztJQUNEOztPQUVHO0lBQ0gsWUFBWTtRQUNSLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7UUFDN0IsSUFBSSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVS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