118 lines
7.7 KiB
JavaScript
118 lines
7.7 KiB
JavaScript
"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");
|
|
const http = require("http");
|
|
// 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* () {
|
|
let done = plugins.smartq.defer();
|
|
if (!this.httpServer) {
|
|
this.httpServer = new http.Server();
|
|
}
|
|
this.io = plugins.socketIo(this.httpServer);
|
|
this.io.on('connection', (socketArg) => {
|
|
this._handleSocketConnection(socketArg);
|
|
});
|
|
this.httpServer.listen(this.options.port, () => {
|
|
done.resolve();
|
|
});
|
|
return yield done.promise;
|
|
});
|
|
}
|
|
/**
|
|
* starts the server with another server
|
|
*/
|
|
setServer(httpServerArg) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
this.httpServer = httpServerArg;
|
|
});
|
|
}
|
|
/**
|
|
* 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb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