nope/lib/communication/layers/ioSocketServerLayer.ts

141 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-04-12 05:09:47 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-22 19:03:15
2022-01-03 18:13:51 +00:00
* @modify date 2022-01-03 17:34:13
2021-04-12 05:09:47 +00:00
* @desc [description]
*/
import * as io from "socket.io";
2022-01-03 18:13:51 +00:00
import {
defineNopeLogger,
ValidLoggerDefinition,
} from "../../logger/getLogger";
import { DEBUG, INFO } from "../../logger/index.browser";
import { Eventnames } from "../../types/nope";
import { EventCommunicationInterface } from "./EventCommunicationInterface";
2021-04-12 05:09:47 +00:00
/**
* Mirror Layer using IO-Sockets.
*
* @export
* @class IoSocketMirrorServer
*/
export class ioSocketServerLayer extends EventCommunicationInterface {
2021-04-12 05:09:47 +00:00
protected _sockets: Set<io.Socket>;
/**
* Creates an instance of IoSocketMirrorServer.
2022-01-03 18:13:51 +00:00
* @author M.Karkowski
* @param {number} port Port the Server is running on.
* @param {ValidLoggerDefinition} [logger="info"]
2021-04-12 05:09:47 +00:00
* @memberof IoSocketMirrorServer
*/
2022-01-03 18:13:51 +00:00
constructor(public port: number, logger: ValidLoggerDefinition = "info") {
2021-04-12 05:09:47 +00:00
super(
// As event Emitter, we provide the IO-Client.
2021-09-04 11:45:52 +00:00
(io as any)({
cors: {
origin: "*",
2021-12-04 07:25:26 +00:00
methods: ["GET", "POST"],
},
2021-09-04 11:45:52 +00:00
}),
2022-01-03 18:13:51 +00:00
defineNopeLogger(logger, "core.mirror.io-srv")
2021-04-12 05:09:47 +00:00
);
2021-04-14 09:58:41 +00:00
const _this = this;
2021-04-12 05:09:47 +00:00
// Tell the Server to listen.
(this._emitter as any).listen(port);
// Now, because we arent connected we set the connected flag to false,
// it will only be true, if a connection with this server has been established
2021-04-14 09:58:41 +00:00
this.connected.getter = () => {
return true;
2021-04-14 09:58:41 +00:00
};
2021-04-12 05:09:47 +00:00
if (_this._logger?.enabledFor(INFO)) {
2022-01-03 18:13:51 +00:00
this._logger.info("Hosting Server on Port " + port.toString());
}
2021-04-12 05:09:47 +00:00
2021-04-13 13:38:01 +00:00
(this._emitter as any).on("connection", (client) => {
if (_this._logger?.enabledFor(INFO)) {
2022-01-03 18:13:51 +00:00
_this._logger.info("New Connection established: " + client.id);
}
2021-04-12 05:09:47 +00:00
_this._sockets.add(client);
// Now Subscribe the Events and make shure we
// are forwarding the data.
for (const event of Eventnames) {
2021-04-12 05:09:47 +00:00
client.on(event, (data) => {
if (event !== "StatusChanged" && _this._logger?.enabledFor(DEBUG)) {
2021-09-04 11:45:52 +00:00
_this._logger.debug(
"forwarding",
"'" + event.toString() + "'",
data
);
}
2021-04-12 05:09:47 +00:00
_this._forward(client, event, data);
});
}
// Subscribe to Loosing connection:
client.on("disconnect", () => {
if (_this._logger?.enabledFor(INFO)) {
2022-01-03 18:13:51 +00:00
_this._logger.info("Connection of : " + client.id + " lost.");
}
2021-04-12 05:09:47 +00:00
_this._sockets.delete(client);
2021-04-14 09:58:41 +00:00
// Force an Update of the connect-flag
_this.connected.forcePublish();
2021-04-12 05:09:47 +00:00
});
2021-04-14 09:58:41 +00:00
// Force an Update of the connect-flag
_this.connected.forcePublish();
2021-04-12 05:09:47 +00:00
});
this._sockets = new Set();
}
/**
* Helper Function, to forward events to the other connected Sockets.
*
* @protected
* @param {io.Socket} socket The socket, which initally received the data.
* @param {string} event the event which was received
* @param {*} data the data, that needs to be forwarded
* @memberof IoSocketMirrorServer
*/
protected _forward(socket: io.Socket, event: string, data: any): void {
// Flag, used to Debug
let forwarded = false;
2021-04-12 05:09:47 +00:00
for (const socketToForward of this._sockets) {
if (socket !== socketToForward) {
socketToForward.emit(event, data);
// If data has been sended, our flag is set to true
forwarded = true;
2021-04-12 05:09:47 +00:00
}
}
// Now we log the output
if (event !== "StatusUpdate" && this._logger?.enabledFor(DEBUG)) {
2021-09-04 11:45:52 +00:00
this._logger.debug(
forwarded ? "forwarded" : "didnt forward",
"'" + event.toString() + "'",
data
);
}
2021-04-12 05:09:47 +00:00
}
2021-10-19 08:01:00 +00:00
dispose(): Promise<void> {
// Disposes the Emitter.
return new Promise<void>((resolve, reject) => {
(this._emitter as any as io.Server).removeAllListeners();
(this._emitter as any as io.Server).close((err) => {
if (err) reject(err);
else resolve();
});
2021-10-19 08:01:00 +00:00
});
}
2021-04-12 05:09:47 +00:00
}