"use strict"; 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()); }); }; 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(); this.options = optionsArg; } /** * starts listening to incoming sockets: */ startServer() { return __awaiter(this, void 0, void 0, function* () { this.io = plugins.socketIo(this.options.port); this.io.on('connection', (socketArg) => { this._handleSocketConnection(socketArg); }); }); } /** * starts the server with another server */ startWithSpecificServer() { return __awaiter(this, void 0, void 0, function* () { }); } /** * closes the server */ closeServer() { return __awaiter(this, void 0, void 0, function* () { yield plugins.smartdelay.delayFor(1000); this.openSockets.forEach((socketObjectArg) => { plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`); socketObjectArg.socket.disconnect(); }); this.openSockets.wipe(); this.io.close(); }); } // 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLGlEQUFnRDtBQUdoRCxVQUFVO0FBQ1YsNkJBQStCO0FBRS9CLGlHQUF5RTtBQUN6RSwyRkFBbUU7QUFPbkU7SUFLRSxZQUFhLFVBQTBDO1FBRnZELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQW9CLENBQUE7UUFDL0MsZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBYyxDQUFBO1FBRXZDLElBQUksQ0FBQyxPQUFPLEdBQUcsVUFBVSxDQUFBO0lBQzNCLENBQUM7SUFFRDs7T0FFRztJQUNHLFdBQVc7O1lBQ2YsSUFBSSxDQUFDLEVBQUUsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUE7WUFDN0MsSUFBSSxDQUFDLEVBQUUsQ0FBQyxFQUFFLENBQUMsWUFBWSxFQUFFLENBQUMsU0FBUyxFQUFFLEVBQUU7Z0JBQ3JDLElBQUksQ0FBQyx1QkFBdUIsQ0FBQyxTQUFTLENBQUMsQ0FBQTtZQUN6QyxDQUFDLENBQUMsQ0FBQTt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