smartsocket/dist/smartsocket.classes.socketconnection.js

106 lines
8.8 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 lik_1 = require("lik");
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
exports.allSocketConnections = new lik_1.Objectmap();
2016-08-09 09:42:21 +00:00
/**
* class SocketConnection represents a websocket connection
*/
2016-08-08 16:20:00 +00:00
class SocketConnection {
constructor(optionsArg) {
this.authenticated = false;
2016-08-08 16:20:00 +00:00
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.side = optionsArg.side;
2016-09-04 22:34:09 +00:00
this.smartsocketHost = optionsArg.smartsocketHost;
2016-08-08 16:20:00 +00:00
this.socket = optionsArg.socket;
2016-08-14 01:25:26 +00:00
// standard behaviour that is always true
exports.allSocketConnections.add(this);
2016-08-14 01:25:26 +00:00
this.socket.on("disconnect", () => {
plugins.beautylog.info(`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`);
exports.allSocketConnections.remove(this);
2016-08-14 01:25:26 +00:00
});
2016-08-08 16:20:00 +00:00
}
2016-08-14 01:25:26 +00:00
;
// authenticating --------------------------
2016-08-09 09:42:21 +00:00
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", (dataArg) => {
2016-08-09 09:42:21 +00:00
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
2016-09-04 22:34:09 +00:00
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
2016-08-09 09:42:21 +00:00
this.alias = dataArg.alias;
this.authenticated = true;
2016-09-04 22:34:09 +00:00
this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
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.authenticated = false;
2016-08-09 09:42:21 +00:00
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
plugins.beautylog.log("function request received");
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) {
plugins.beautylog.ok("function in access scope");
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) => {
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg);
});
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
done.resolve(this);
2016-08-09 09:42:21 +00:00
}
else {
let errMessage;
plugins.beautylog.error(errMessage);
done.reject(errMessage);
2016-08-09 09:42:21 +00:00
}
;
return done.promise;
}
;
2016-08-08 16:20:00 +00:00
}
exports.SocketConnection = SocketConnection;
;
2016-09-04 22:34:09 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN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