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
|
2021-10-19 08:01:00 +00:00
|
|
|
* @modify date 2021-10-19 09:10:42
|
2020-11-04 16:39:20 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
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
|
|
|
/**
|
2021-04-12 05:09:47 +00:00
|
|
|
* Mirror Layer using IO-Sockets.
|
2020-12-04 20:39:18 +00:00
|
|
|
*
|
|
|
|
* @export
|
2021-04-12 05:09:47 +00:00
|
|
|
* @class IoSocketMirrorClient
|
2020-12-04 20:39:18 +00:00
|
|
|
*/
|
2021-04-12 05:09:47 +00:00
|
|
|
export class IoSocketMirrorClient extends EventMirror {
|
2021-03-19 18:17:39 +00:00
|
|
|
/**
|
2021-04-12 05:09:47 +00:00
|
|
|
* Creates an instance of IoSocketMirrorClient.
|
2021-03-19 18:17:39 +00:00
|
|
|
* @param {string} uri Uri of the Server.
|
|
|
|
* @param {LoggerLevel} [level="info"] Logger level
|
2021-04-12 05:09:47 +00:00
|
|
|
* @memberof IoSocketMirrorClient
|
2021-03-19 18:17:39 +00:00
|
|
|
*/
|
2021-03-22 19:24:15 +00:00
|
|
|
constructor(public uri: string, level: LoggerLevel = "info") {
|
|
|
|
super(
|
2021-04-12 05:09:47 +00:00
|
|
|
// As event Emitter, we provide the IO-Client.
|
2021-03-22 19:24:15 +00:00
|
|
|
connect(uri.startsWith("http://") ? uri : "http://" + uri) as any,
|
|
|
|
getNopeLogger("io-mirror-client", level)
|
|
|
|
);
|
2020-12-30 18:53:33 +00:00
|
|
|
|
2021-04-12 05:09:47 +00:00
|
|
|
// Make shure we use the http as starting of the uri.
|
2020-12-04 20:39:18 +00:00
|
|
|
this.uri = this.uri.startsWith("http://") ? this.uri : "http://" + this.uri;
|
2021-08-17 15:52:46 +00:00
|
|
|
this.receivesOwnMessages = false;
|
2021-04-12 05:09:47 +00:00
|
|
|
|
|
|
|
// 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.
|
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);
|
|
|
|
});
|
|
|
|
}
|
2021-10-19 08:01:00 +00:00
|
|
|
|
|
|
|
async dispose(): Promise<void> {
|
|
|
|
// Disposes the Emitter.
|
|
|
|
(this._emitter as any as SocketIOClient.Socket).removeAllListeners();
|
|
|
|
(this._emitter as any as SocketIOClient.Socket).disconnect();
|
|
|
|
}
|
2020-12-30 18:53:33 +00:00
|
|
|
}
|