90 lines
5.8 KiB
JavaScript
90 lines
5.8 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();
|
|
this.socketRoles = 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",
|
|
smartsocketHost: this,
|
|
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;
|
|
}
|
|
;
|
|
/**
|
|
* adds socketRoles
|
|
*/
|
|
addSocketRoles(socketRolesArray) {
|
|
for (let socketRole of socketRolesArray) {
|
|
this.socketRoles.add(socketRole);
|
|
}
|
|
;
|
|
return;
|
|
}
|
|
;
|
|
}
|
|
exports.Smartsocket = Smartsocket;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFHakQsVUFBVTtBQUNWLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQUVoQyx1REFBaUMsd0NBQXdDLENBQUMsQ0FBQTtBQUMxRSxvREFBOEIscUNBQXFDLENBQUMsQ0FBQTtBQUtuRSxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQTBDO1FBRnRELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQW9CLENBQUM7UUFDaEQsZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBYyxDQUFDO1FBNEIxQzs7V0FFRztRQUNILGdCQUFXLEdBQUc7WUFDVixJQUFJLENBQUMsRUFBRSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztZQUM5QyxJQUFJLENBQUMsRUFBRSxDQ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