2016-08-07 12:58:20 +00:00
|
|
|
"use strict";
|
2017-07-15 21:16:22 +00:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
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");
|
2017-10-09 08:40:59 +00:00
|
|
|
const http = require("http");
|
2016-08-07 12:58:20 +00:00
|
|
|
// 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 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();
|
2017-07-15 21:16:22 +00:00
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* starts listening to incoming sockets:
|
|
|
|
*/
|
|
|
|
startServer() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-10-09 08:40:59 +00:00
|
|
|
let done = plugins.smartq.defer();
|
|
|
|
if (!this.httpServer) {
|
|
|
|
this.httpServer = new http.Server();
|
|
|
|
}
|
|
|
|
this.io = plugins.socketIo(this.httpServer);
|
2018-03-15 01:29:40 +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
|
|
|
});
|
2017-10-09 08:40:59 +00:00
|
|
|
this.httpServer.listen(this.options.port, () => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
return yield done.promise;
|
2017-07-15 21:16:22 +00:00
|
|
|
});
|
|
|
|
}
|
2017-10-09 08:28:18 +00:00
|
|
|
/**
|
|
|
|
* starts the server with another server
|
|
|
|
*/
|
2017-10-09 08:40:59 +00:00
|
|
|
setServer(httpServerArg) {
|
2017-10-09 08:28:18 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-10-09 08:40:59 +00:00
|
|
|
this.httpServer = httpServerArg;
|
2017-10-09 08:28:18 +00:00
|
|
|
});
|
|
|
|
}
|
2017-07-15 21:16:22 +00:00
|
|
|
/**
|
|
|
|
* closes the server
|
|
|
|
*/
|
|
|
|
closeServer() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-10-09 08:28:18 +00:00
|
|
|
yield plugins.smartdelay.delayFor(1000);
|
2016-08-07 12:58:20 +00:00
|
|
|
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();
|
2017-07-15 21:16:22 +00:00
|
|
|
});
|
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) {
|
2017-07-07 20:02:19 +00:00
|
|
|
let done = plugins.smartq.defer();
|
2016-08-15 00:36:17 +00:00
|
|
|
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
|
2017-07-07 20:02:19 +00:00
|
|
|
side: 'requesting',
|
2016-08-15 00:36:17 +00:00
|
|
|
originSocketConnection: targetSocketConnectionArg,
|
|
|
|
shortId: plugins.shortid.generate(),
|
|
|
|
funcCallData: {
|
|
|
|
funcName: functionNameArg,
|
|
|
|
funcDataArg: dataArg
|
|
|
|
}
|
|
|
|
});
|
2018-03-15 01:29:40 +00:00
|
|
|
socketRequest.dispatch().then((dataArg) => {
|
2016-08-15 00:36:17 +00:00
|
|
|
done.resolve(dataArg.funcDataArg);
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-08-12 03:56:40 +00:00
|
|
|
}
|
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);
|
2018-03-15 01:29:40 +00:00
|
|
|
socketConnection
|
|
|
|
.authenticate()
|
2017-07-07 20:02:19 +00:00
|
|
|
.then(() => {
|
|
|
|
return socketConnection.listenToFunctionRequests();
|
|
|
|
})
|
2018-03-15 01:29:40 +00:00
|
|
|
.catch(err => {
|
2017-07-07 20:02:19 +00:00
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
}
|
2016-08-07 12:58:20 +00:00
|
|
|
}
|
|
|
|
exports.Smartsocket = Smartsocket;
|
2018-03-15 01:29:40 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLGlEQUFpRDtBQUdqRCw2QkFBNkI7QUFFN0IsVUFBVTtBQUNWLDZCQUFnQztBQUVoQyxpR0FBMEU7QUFDMUUsMkZBQW9FO0FBVXBFO0lBTUUsWUFBWSxVQUEwQztRQUZ0RCxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFvQixDQUFDO1FBQ2hELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQztRQUV4QyxJQUFJLENBQUMsT0FBTyxHQUFHLFVBQVUsQ0FBQztJQUM1QixDQUFDO0lBRUQ7O09BRUc7SUFDRyxXQUFXOztZQUNmLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUM7WUFDbEMsRUFBRSxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztnQkFDckIsSUFBSSxDQUFDLFVBQVUsR0FBRyxJQUFJLElBQUksQ0FBQyxNQUFNLEVBQUUsQ0FBQztZQUN0QyxDQUFDO1lBQ0QsSUFBSSxDQUFDLEVBQUUsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLElBQUksQ0FBQyxVQUFV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