2020-11-04 16:39:20 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-04 17:36:04
|
2021-03-04 11:33:53 +00:00
|
|
|
* @modify date 2021-03-01 17:41:45
|
2020-11-04 16:39:20 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-11-23 06:09:31 +00:00
|
|
|
import { EventEmitter } from "events";
|
2020-12-04 20:39:18 +00:00
|
|
|
import * as io from "socket.io";
|
|
|
|
import { connect } from "socket.io-client";
|
2020-11-12 16:07:05 +00:00
|
|
|
import { getNopeLogger } from "../logger/getLogger";
|
2021-02-05 10:53:33 +00:00
|
|
|
import { ICommunicationInterface } from "../types/nope/nopeCommunication.interface";
|
2020-12-04 20:39:18 +00:00
|
|
|
import { Bridge } from "./bridge";
|
2021-03-01 16:26:18 +00:00
|
|
|
import { DebuggedEventLayer } from "./debugEventLayer";
|
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-02-05 10:53:33 +00:00
|
|
|
//@ts-ignore Ignore the Interface. Its implemented manually
|
|
|
|
export class IoSocketClient extends Bridge implements ICommunicationInterface {
|
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
|
|
|
|
*/
|
2020-12-04 18:10:33 +00:00
|
|
|
constructor(public uri: string) {
|
2020-12-30 18:53:33 +00:00
|
|
|
super(
|
|
|
|
false,
|
|
|
|
getNopeLogger("io-socket-client", "info")
|
|
|
|
);
|
|
|
|
|
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
|
|
|
const _this = this;
|
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
this.connected.getter = (value) => value;
|
2020-12-04 18:10:33 +00:00
|
|
|
this.connected.setContent(false);
|
|
|
|
|
|
|
|
this._logger.info("connecting to: " + uri);
|
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
const socket = connect(uri);
|
2020-12-04 18:10:33 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
// Add a default Event-Layer
|
|
|
|
// Perhaps that can be removed.
|
2020-12-30 18:53:33 +00:00
|
|
|
this.addLayer(
|
2021-03-01 16:26:18 +00:00
|
|
|
new DebuggedEventLayer(
|
2020-12-30 18:53:33 +00:00
|
|
|
new EventEmitter(),
|
|
|
|
"individual",
|
|
|
|
"individual",
|
2021-03-04 11:33:53 +00:00
|
|
|
this._logger
|
2021-03-01 16:26:18 +00:00
|
|
|
),
|
|
|
|
false,
|
|
|
|
true
|
2020-12-30 18:53:33 +00:00
|
|
|
);
|
|
|
|
this.addLayer(
|
2021-03-01 16:26:18 +00:00
|
|
|
new DebuggedEventLayer(socket as any, "individual", "individual", this._logger)
|
2020-12-30 18:53:33 +00:00
|
|
|
);
|
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
socket.on("connect", (...args) => {
|
2020-12-04 18:10:33 +00:00
|
|
|
// Element is connected
|
|
|
|
_this._logger.info("connected");
|
|
|
|
_this.connected.setContent(true);
|
|
|
|
});
|
2021-02-05 10:53:33 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
socket.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
|
|
|
}
|