nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
114 lines (113 loc) • 4.53 kB
JavaScript
;
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.IoHostLayer = void 0;
const io = require("socket.io");
const getLogger_1 = require("../../logger/getLogger");
const index_browser_1 = require("../../logger/index.browser");
const EventCommunicationInterface_1 = require("./EventCommunicationInterface");
/**
* Mirror Layer using IO-Sockets.
*
* @export
* @class IoSocketMirrorClient
*/
class IoHostLayer extends EventCommunicationInterface_1.EventCommunicationInterface {
/**
* Creates an instance of IoSocketMirrorClient.
* @author M.Karkowski
* @param {string} uri
* @param {ValidLoggerDefinition} [logger="info"]
* @memberof IoSocketMirrorClient
*/
constructor(_bridge, port, logger = "info", shareData = false) {
var _a;
super(
// As event Emitter, we provide the IO-Client.
io({
cors: {
origin: "*",
methods: ["GET", "POST"],
},
}), (0, getLogger_1.defineNopeLogger)(logger, "core.layer.io-host"), false);
this._bridge = _bridge;
this.port = port;
this.shareData = shareData;
this._openRequests = {};
const _this = this;
// Tell the Server to listen.
this._emitter.listen(port);
// Now, because we arent connected we set the connected flag to false,
// it will only be true, if a connection with this server has been established
this.connected.getter = () => {
return true;
};
if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) {
this._logger.info("Hosting Server on Port " + port.toString());
}
this._emitter.on("connection", (client) => {
var _a;
if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) {
_this._logger.info("New Connection established: " + client.id);
}
/// Create an Event interface. This we will use as "new layer"
const nopeIoLayer = new EventCommunicationInterface_1.EventCommunicationInterface(client, this._logger, false);
_this._sockets.set(client, nopeIoLayer);
_this._bridge.addCommunicationLayer(nopeIoLayer).catch((e) => {
if (_this._logger) {
_this._logger.error("IO-Host failed to add new client to bridge !");
_this._logger.error(e);
}
});
// Subscribe to Loosing connection:
client.on("disconnect", () => {
var _a;
if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) {
_this._logger.info("Connection of : " + client.id + " lost.");
}
_this._sockets.delete(client);
_this._bridge.removeCommunicationLayer(nopeIoLayer).catch((e) => {
if (_this._logger) {
_this._logger.error("IO-Host failed to remove client from bridge !");
_this._logger.error(e);
}
});
// Force an Update of the connect-flag
_this.connected.forcePublish();
});
// Force an Update of the connect-flag
_this.connected.forcePublish();
});
this._sockets = new Map();
}
/**
* Function, which will be used to emit data
*
* @param {ValidEventTypesOfMirror} event the name fo the event to emit something
* @param {*} data the data to emit
* @memberof EventMirror
*/
async emit(eventname, data) {
const promises = new Array();
for (const client of this._sockets.values()) {
promises.push(client.emit(eventname, data));
}
await Promise.all(promises);
}
dispose() {
// Disposes the Emitter.
return new Promise((resolve, reject) => {
this._emitter.removeAllListeners();
this._emitter.close((err) => {
if (err)
reject(err);
else
resolve();
});
});
}
}
exports.IoHostLayer = IoHostLayer;