nope/lib/communication/mirrors/ioSocketMirror.ts
Martin Karkowski c02d08b408 Fixing Layers
2021-03-24 07:50:36 +01:00

65 lines
1.8 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-22 19:03:15
* @modify date 2021-03-23 08:00:49
* @desc [description]
*/
import * as io from "socket.io";
import { connect } from "socket.io-client";
import { getNopeLogger } from "../../logger/getLogger";
import { LoggerLevel } from "../../logger/nopeLogger";
import { EventMirror } from "./eventMirror";
/**
* Communication Layer using IO-Sockets.
*
* @export
* @class IoSocketServer
* @implements {ICommunicationInterface}
*/
export class IoSocketMirror extends EventMirror {
protected _socket: io.Server;
protected _sockets: Set<io.Socket>;
/**
* Creates an instance of IoSocketServer.
* @param {number} port The Port, on which the Server should be hosted
* @param {Server} [server] A Server shich should be used. (Otherwise a Server is created)
* @memberof IoSocketServer
*/
/**
* Creates an instance of IoSocketClient.
* @param {string} uri Uri of the Server.
* @param {LoggerLevel} [level="info"] Logger level
* @memberof IoSocketClient
*/
constructor(public uri: string, level: LoggerLevel = "info") {
super(
connect(uri.startsWith("http://") ? uri : "http://" + uri) as any,
getNopeLogger("io-mirror-client", level)
);
// Make shure we use the http before connecting.
this.uri = this.uri.startsWith("http://") ? this.uri : "http://" + this.uri;
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);
});
}
}