2020-11-04 16:39:20 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
2021-03-22 19:24:15 +00:00
|
|
|
* @create date 2021-03-22 19:03:15
|
|
|
|
* @modify date 2021-03-22 19:12:16
|
2020-11-04 16:39:20 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
import * as io from "socket.io";
|
|
|
|
import { connect } from "socket.io-client";
|
2021-03-22 19:24:15 +00:00
|
|
|
import { getNopeLogger } from "../../logger/getLogger";
|
|
|
|
import { LoggerLevel } from "../../logger/nopeLogger";
|
|
|
|
import { EventMirror } from "./eventMirror";
|
2020-12-04 18:10:33 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
/**
|
|
|
|
* Communication Layer using IO-Sockets.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class IoSocketServer
|
|
|
|
* @implements {ICommunicationInterface}
|
|
|
|
*/
|
2021-03-22 19:24:15 +00:00
|
|
|
export class IoSocketClient extends EventMirror {
|
2020-12-04 20:39:18 +00:00
|
|
|
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
|
|
|
|
*/
|
2021-03-19 18:17:39 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of IoSocketClient.
|
|
|
|
* @param {string} uri Uri of the Server.
|
|
|
|
* @param {LoggerLevel} [level="info"] Logger level
|
|
|
|
* @memberof IoSocketClient
|
|
|
|
*/
|
2021-03-22 19:24:15 +00:00
|
|
|
constructor(public uri: string, level: LoggerLevel = "info") {
|
|
|
|
super(
|
|
|
|
connect(uri.startsWith("http://") ? uri : "http://" + uri) as any,
|
|
|
|
getNopeLogger("io-mirror-client", level)
|
|
|
|
);
|
2020-12-30 18:53:33 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
// Make shure we use the http before connecting.
|
|
|
|
this.uri = this.uri.startsWith("http://") ? this.uri : "http://" + this.uri;
|
2020-12-04 18:10:33 +00:00
|
|
|
this.connected.setContent(false);
|
|
|
|
|
|
|
|
this._logger.info("connecting to: " + uri);
|
|
|
|
|
2021-03-22 19:24:15 +00:00
|
|
|
const _this = this;
|
2021-03-19 18:17:39 +00:00
|
|
|
|
2021-03-22 19:24:15 +00:00
|
|
|
this._emitter.on("connect", (...args) => {
|
2020-12-04 18:10:33 +00:00
|
|
|
// Element is connected
|
|
|
|
_this._logger.info("connected");
|
|
|
|
_this.connected.setContent(true);
|
|
|
|
});
|
2021-03-19 18:17:39 +00:00
|
|
|
|
2021-03-22 19:24:15 +00:00
|
|
|
this._emitter.on("disconnect", () => {
|
2020-12-04 18:10:33 +00:00
|
|
|
// Connection Lost.
|
|
|
|
_this._logger.error("Connection lost!");
|
|
|
|
_this.connected.setContent(false);
|
|
|
|
});
|
|
|
|
}
|
2020-12-30 18:53:33 +00:00
|
|
|
}
|