nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
110 lines (109 loc) • 4.26 kB
JavaScript
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
import * as io from "socket.io";
import { defineNopeLogger, } from "../../logger/getLogger";
import { INFO } from "../../logger/index.browser";
import { EventCommunicationInterface } from "./EventCommunicationInterface";
/**
* Mirror Layer using IO-Sockets.
*
* @export
* @class IoSocketMirrorClient
*/
export class IoHostLayer extends 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"],
},
}), 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(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(INFO)) {
_this._logger.info("New Connection established: " + client.id);
}
/// Create an Event interface. This we will use as "new layer"
const nopeIoLayer = new 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(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();
});
});
}
}