smartsocket/dist/smartsocket.classes.smartsocket.js

85 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
"use strict";
2017-07-07 20:02:19 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2016-08-07 12:58:20 +00:00
const plugins = require("./smartsocket.plugins");
// classes
const lik_1 = require("lik");
2016-08-08 16:20:00 +00:00
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
2016-08-07 12:58:20 +00:00
class Smartsocket {
2016-08-07 16:59:39 +00:00
constructor(optionsArg) {
2016-08-07 12:58:20 +00:00
this.openSockets = new lik_1.Objectmap();
2016-09-04 22:34:09 +00:00
this.socketRoles = new lik_1.Objectmap();
2016-08-07 16:59:39 +00:00
/**
* starts listening to incling sockets:
*/
this.startServer = () => {
this.io = plugins.socketIo(this.options.port);
2017-07-07 20:02:19 +00:00
this.io.on('connection', (socketArg) => {
this._handleSocketConnection(socketArg);
2016-08-07 16:59:39 +00:00
});
};
2016-08-07 12:58:20 +00:00
this.closeServer = () => {
this.openSockets.forEach((socketObjectArg) => {
2016-08-07 16:59:39 +00:00
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
2016-08-07 12:58:20 +00:00
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
2016-08-07 16:59:39 +00:00
this.io.close();
2016-08-07 12:58:20 +00:00
};
2016-08-07 16:59:39 +00:00
this.options = optionsArg;
2016-08-07 12:58:20 +00:00
}
// communication
/**
* allows call to specific client.
*/
clientCall(functionNameArg, dataArg, targetSocketConnectionArg) {
2017-07-07 20:02:19 +00:00
let done = plugins.smartq.defer();
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
2017-07-07 20:02:19 +00:00
side: 'requesting',
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
});
socketRequest.dispatch()
.then((dataArg) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
}
2016-09-04 22:34:09 +00:00
/**
* adds socketRoles
*/
addSocketRoles(socketRolesArray) {
for (let socketRole of socketRolesArray) {
this.socketRoles.add(socketRole);
}
return;
}
2017-07-07 20:02:19 +00:00
/**
* 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);
});
}
2016-08-07 12:58:20 +00:00
}
exports.Smartsocket = Smartsocket;
2017-07-07 20:02:19 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpREFBZ0Q7QUFHaEQsVUFBVTtBQUNWLDZCQUErQjtBQUUvQixpR0FBeUU7QUFDekUsMkZBQW1FO0FBT25FO0lBS0UsWUFBYSxVQUEwQztRQUZ2RCxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFvQixDQUFBO1FBQy9DLGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQTtRQUt6Qzs7V0FFRztRQUNILGdCQUFXLEdBQUc7WUFDWixJQUFJLENBQUMsRUFBRSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQTtZQUM3QyxJQUFJLENBQUMsRUFBRS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