UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

57 lines (56 loc) 2.23 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de */ Object.defineProperty(exports, "__esModule", { value: true }); exports.IoSocketClientLayer = void 0; const socket_io_client_1 = require("socket.io-client"); const getLogger_1 = require("../../logger/getLogger"); const EventCommunicationInterface_1 = require("./EventCommunicationInterface"); /** * Mirror Layer using IO-Sockets. * * @export * @class IoSocketMirrorClient */ class IoSocketClientLayer extends EventCommunicationInterface_1.EventCommunicationInterface { /** * Creates an instance of IoSocketMirrorClient. * @author M.Karkowski * @param {string} uri * @param {ValidLoggerDefinition} [logger="info"] * @memberof IoSocketMirrorClient */ constructor(uri, logger = "info") { super( // As event Emitter, we provide the IO-Client. (0, socket_io_client_1.connect)(uri.startsWith("http://") ? uri : "http://" + uri), (0, getLogger_1.defineNopeLogger)(logger, "core.layer.io"), false); this.uri = uri; // Make shure we use the http as starting of the uri. this.uri = this.uri.startsWith("http://") ? this.uri : "http://" + this.uri; // Now, because we arent connected we set the connected flag to false, // it will only be true, if a connection with the server has been established // Therefore we will connect to the "connect" and "disconnect" event of the // socket. this.connected.setContent(false); this._logger.info("connecting to: " + uri); const _this = this; this._emitter.on("connect", (...args) => { // Element is connected _this._logger.info("connected"); _this.connected.setContent(true); }); this._emitter.on("disconnect", () => { // Connection Lost. _this._logger.error("Connection lost!"); _this.connected.setContent(false); }); } async dispose() { // Disposes the Emitter. this._emitter.removeAllListeners(); this._emitter.disconnect(); } } exports.IoSocketClientLayer = IoSocketClientLayer;