smartsocket/dist/smartsocket.classes.smartsocket.js

111 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
"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());
});
};
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");
2018-03-19 09:00:11 +00:00
const smartsocket_classes_socketserver_1 = require("./smartsocket.classes.socketserver");
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();
2018-03-19 09:00:11 +00:00
this.socketServer = new smartsocket_classes_socketserver_1.SocketServer(this);
// tslint:disable-next-line:member-ordering
this.setExternalServer = this.socketServer.setExternalServer;
this.options = optionsArg;
}
/**
2018-03-19 09:00:11 +00:00
* starts smartsocket
*/
2018-03-19 09:00:11 +00:00
start() {
return __awaiter(this, void 0, void 0, function* () {
2018-03-19 09:00:11 +00:00
this.io = plugins.socketIo(this.socketServer.getServerForSocketIo());
yield this.socketServer.start();
2018-03-15 01:29:40 +00:00
this.io.on('connection', socketArg => {
this._handleSocketConnection(socketArg);
2016-08-07 16:59:39 +00:00
});
2017-10-09 08:28:18 +00:00
});
}
/**
2018-03-19 09:00:11 +00:00
* stops smartsocket
*/
2018-03-19 09:00:11 +00:00
stop() {
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();
2018-03-19 09:00:11 +00:00
// stop the corresponging server
this.socketServer.stop();
});
2016-08-07 12:58:20 +00:00
}
// communication
/**
* allows call to specific client.
*/
clientCall(functionNameArg, dataArg, targetSocketConnectionArg) {
2018-03-19 09:00:11 +00:00
return __awaiter(this, void 0, void 0, function* () {
const done = plugins.smartq.defer();
const socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
side: 'requesting'
});
socketRequest.dispatch().then((dataArg) => {
done.resolve(dataArg.funcDataArg);
});
const result = yield done.promise;
return result;
});
}
2016-09-04 22:34:09 +00:00
/**
* adds socketRoles
*/
addSocketRoles(socketRolesArray) {
2018-03-19 09:00:11 +00:00
for (const socketRole of socketRolesArray) {
2016-09-04 22:34:09 +00:00
this.socketRoles.add(socketRole);
}
return;
}
2017-07-07 20:02:19 +00:00
/**
* the standard handler for new socket connections
*/
_handleSocketConnection(socketArg) {
2018-03-19 09:00:11 +00:00
const socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
2017-07-07 20:02:19 +00:00
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-19 09:00:11 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLGlEQUFpRDtBQUdqRCxVQUFVO0FBQ1YsNkJBQWdDO0FBQ2hDLGlHQUEwRTtBQUUxRSwyRkFBb0U7QUFFcEUseUZBQWtFO0FBU2xFO0lBUUUsWUFBWSxVQUEwQztRQUwvQyxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFvQixDQUFDO1FBQ2hELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQztRQUV6QyxpQkFBWSxHQUFHLElBQUksK0NBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQztRQU05QywyQ0FBMkM7UUFDcEMsc0JBQWlCLEdBQUcsSUFBSSxDQUFDLFlBQVksQ0FBQyxpQkFBaUIsQ0FBQztRQUo3RCxJQUFJLENBQUMsT0FBTyxHQUFHLFVBQVUsQ0FBQztJQUM1QixDQUFDO0lBS0Q7O09BRUc7SUFDVSxLQUFLOztZQUNoQixJQUFJLENBQUMsRUFBRSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLFlBQVksQ0FBQyxvQkFBb0IsRUFBRSxDQUFDLENBQUM7WUFDckUsTUFBTSxJQUFJLENBQUMsWUFBWSxDQUFDLEtBQUssRUFBRSxDQUFDO1lBQ2hDLElBQUksQ0FBQyxFQUFFLENBQU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