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-19 18:17:39 +00:00
|
|
|
* @modify date 2021-03-19 14:35:58
|
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-19 18:17:39 +00:00
|
|
|
import { generateId } from "../helpers/idMethods";
|
|
|
|
import { LoggerLevel } from "../logger/nopeLogger";
|
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-19 18:17:39 +00:00
|
|
|
import { EventLayer } from "./eventLayer";
|
|
|
|
import { MirrorLayer } from "./mirrorLayer";
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
public uri: string,
|
|
|
|
mode: "mirror" | "default" = "default",
|
|
|
|
level: LoggerLevel = "info"
|
|
|
|
) {
|
|
|
|
super(generateId(), "io-socket-client " + uri, 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
|
|
|
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);
|
|
|
|
|
2021-03-19 18:17:39 +00:00
|
|
|
// 1. Create the Socket.
|
2020-12-04 20:39:18 +00:00
|
|
|
const socket = connect(uri);
|
2021-03-19 18:17:39 +00:00
|
|
|
// 2. Create the Layer. Based on the Mode it will be either an Mirror or Default Layer.
|
|
|
|
const layer =
|
|
|
|
mode === "default"
|
|
|
|
? new EventLayer(socket as any, this._logger)
|
|
|
|
: new MirrorLayer(socket as any, this._logger);
|
|
|
|
// 3. Add the Layer
|
|
|
|
this.addLayer(layer);
|
2020-12-30 18:53:33 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
socket.on("connect", (...args) => {
|
2021-03-19 18:17:39 +00:00
|
|
|
// Assign new ID.
|
|
|
|
(_this._logger as any).context.name += "_" + socket.id;
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
// Element is connected
|
|
|
|
_this._logger.info("connected");
|
2021-03-19 18:17:39 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
_this.connected.setContent(true);
|
|
|
|
});
|
2021-03-19 18:17:39 +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
|
|
|
}
|