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");
|
2016-08-15 00:36:17 +00:00
|
|
|
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-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) => {
|
2016-08-12 03:56:40 +00:00
|
|
|
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
|
|
|
|
*/
|
2016-08-12 03:56:40 +00:00
|
|
|
_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,
|
2016-08-15 00:36:17 +00:00
|
|
|
side: "server",
|
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()
|
2016-08-14 23:38:28 +00:00
|
|
|
.then(() => {
|
|
|
|
return socketConnection.listenToFunctionRequests();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2016-08-07 12:58:20 +00:00
|
|
|
}
|
|
|
|
;
|
2016-08-12 03:56:40 +00:00
|
|
|
// communication
|
2016-08-15 00:36:17 +00:00
|
|
|
/**
|
|
|
|
* 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-08-12 03:56:40 +00:00
|
|
|
}
|
2016-08-07 12:58:20 +00:00
|
|
|
}
|
|
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