smartsocket/dist/smartsocket.classes.smartsocket.js

76 lines
5.1 KiB
JavaScript

"use strict";
const plugins = require("./smartsocket.plugins");
// classes
const lik_1 = require("lik");
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
;
class Smartsocket {
constructor(optionsArg) {
this.openSockets = new lik_1.Objectmap();
/**
* starts listening to incling sockets:
*/
this.startServer = () => {
this.io = plugins.socketIo(this.options.port);
this.io.on("connection", (socketArg) => {
this._handleSocketConnection(socketArg);
});
};
this.closeServer = () => {
this.openSockets.forEach((socketObjectArg) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
this.io.close();
};
this.options = optionsArg;
}
;
/**
* the standard handler for new socket connections
*/
_handleSocketConnection(socketArg) {
let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
alias: undefined,
authenticated: false,
role: undefined,
side: "server",
socket: socketArg
});
plugins.beautylog.log("Socket connected. Trying to authenticate...");
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch((err) => {
console.log(err);
});
}
;
// communication
/**
* allows call to specific client.
*/
clientCall(functionNameArg, dataArg, targetSocketConnectionArg) {
let done = plugins.q.defer();
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
side: "requesting",
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
});
socketRequest.dispatch()
.then((dataArg) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
}
}
exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3Rz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