smartsocket/dist/smartsocket.classes.smartsocket.js

90 lines
5.8 KiB
JavaScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
"use strict";
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 16:59:39 +00:00
;
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);
2016-08-14 01:25:26 +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
}
;
2016-08-07 16:59:39 +00:00
/**
* the standard handler for new socket connections
*/
_handleSocketConnection(socketArg) {
2016-08-08 16:20:00 +00:00
let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
2016-08-14 01:25:26 +00:00
alias: undefined,
2016-08-08 16:20:00 +00:00
authenticated: false,
2016-08-14 01:25:26 +00:00
role: undefined,
side: "server",
2016-09-04 22:34:09 +00:00
smartsocketHost: this,
2016-08-08 16:20:00 +00:00
socket: socketArg
});
2016-08-07 16:59:39 +00:00
plugins.beautylog.log("Socket connected. Trying to authenticate...");
2016-08-08 16:20:00 +00:00
this.openSockets.add(socketConnection);
2016-08-09 09:42:21 +00:00
socketConnection.authenticate()
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch((err) => {
console.log(err);
});
2016-08-07 12:58:20 +00:00
}
;
// 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;
}
2016-09-04 22:34:09 +00:00
;
/**
* adds socketRoles
*/
addSocketRoles(socketRolesArray) {
for (let socketRole of socketRolesArray) {
this.socketRoles.add(socketRole);
}
;
return;
}
;
2016-08-07 12:58:20 +00:00
}
exports.Smartsocket = Smartsocket;
2016-09-04 22:34:09 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFHakQsVUFBVTtBQUNWLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQUVoQyx1REFBaUMsd0NBQXdDLENBQUMsQ0FBQTtBQUMxRSxvREFBOEIscUNBQXFDLENBQUMsQ0FBQTtBQUtuRSxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQTBDO1FBRnRELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQW9CLENBQUM7UUF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