85 lines
5.6 KiB
JavaScript
85 lines
5.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
}
|
|
// communication
|
|
/**
|
|
* allows call to specific client.
|
|
*/
|
|
clientCall(functionNameArg, dataArg, targetSocketConnectionArg) {
|
|
let done = plugins.smartq.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;
|
|
}
|
|
/**
|
|
* 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);
|
|
});
|
|
}
|
|
}
|
|
exports.Smartsocket = Smartsocket;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBZ0Q7QUFHaEQsVUFBVTtBQUNWLDZCQUErQjtBQUUvQixpR0FBeUU7QUFDekUsMkZBQW1FO0FBT25FO0lBS0UsWUFBYSxVQUEwQztRQUZ2RCxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFvQixDQUFBO1FBQy9DLGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQTtRQUt6Qzs7V0FFRztRQUNILGdCQUFXLEdBQUc7WUFDWixJQUFJLENBQUMsRUFBRSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQTtZQUM3QyxJQUFJLENBQUMsRUFBRSxDQUFDLEVBQUUsQ0FBQyxZQUFZLEVBQUUsQ0FBQyxTQUFTO2dCQUNqQyxJQUFJLENBQUMsdUJBQXVCLENBQUMsU0FBUyxDQUFDLENBQUE7WUFDekMs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