nope/lib/communication/layers/IoSocketServer.ts

107 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:42
2021-03-04 11:33:53 +00:00
* @modify date 2021-03-01 17:43:12
2020-11-06 08:10:30 +00:00
* @desc [description]
*/
2020-08-23 07:21:03 +00:00
import { Server } from "http";
2020-11-23 06:09:31 +00:00
import * as io from "socket.io";
2021-03-22 19:24:15 +00:00
import { generateId } from "../../helpers/idMethods";
import { getNopeLogger } from "../../logger/getLogger";
import { LoggerLevel } from "../../logger/nopeLogger";
import { Bridge } from "../bridge";
2021-03-19 18:17:39 +00:00
import { EventLayer } from "./eventLayer";
2020-08-23 07:21:03 +00:00
2020-09-11 12:07:31 +00:00
/**
* Communication Layer using IO-Sockets.
*
* @export
* @class IoSocketServer
* @implements {ICommunicationInterface}
2020-09-11 12:07:31 +00:00
*/
export class IoSocketServer extends Bridge {
protected _socket: io.Server;
2020-11-04 16:39:20 +00:00
protected _sockets: Set<io.Socket>;
/**
* Creates an instance of IoSocketServer.
* @param {number} port The Port, on which the Server should be hosted
2021-03-19 18:17:39 +00:00
* @param {("mirror" | "default")} [mode="default"] The Mode of the Server (it could be either a mirror-server or default-server)
* @param {LoggerLevel} [level="info"] Logger Level of the Layer
2020-11-04 16:39:20 +00:00
* @param {Server} [server] A Server shich should be used. (Otherwise a Server is created)
* @memberof IoSocketServer
*/
2021-03-19 18:17:39 +00:00
constructor(
public port: number,
level: LoggerLevel = "info",
server?: Server
) {
super(generateId(), "socket-io-server", level);
// If a server has been provided, we will use this, otherwise,
// we will create one.
2020-11-04 16:39:20 +00:00
if (server) {
this._socket = (io as any)(server);
} else {
2021-09-03 05:42:37 +00:00
this._socket = (io as any)({
allowRequest: (req, callback) => {
const noOriginHeader = req.headers.origin === undefined;
callback(null, true);
2021-12-04 07:25:26 +00:00
},
2021-09-03 05:42:37 +00:00
});
2020-11-04 16:39:20 +00:00
}
const _this = this;
this.connected.setContent(false);
this._logger.info(
"waiting for connection. Listening on port: " + port.toString()
);
2020-11-04 16:39:20 +00:00
this._socket.listen(port);
2021-03-19 18:17:39 +00:00
2020-11-23 06:09:31 +00:00
this._socket.on("connection", (client) => {
_this._logger.info("New Connection established: " + client.id);
2020-11-04 16:39:20 +00:00
2021-03-19 18:17:39 +00:00
// 1. Define a Custom Logger for the Client:
const logger = getNopeLogger("io-client-" + client.id, "info");
2020-11-04 16:39:20 +00:00
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.
2021-03-22 19:24:15 +00:00
const layer = new EventLayer(client as any, logger);
2021-03-19 18:17:39 +00:00
// 3. Add the Layer
this.addLayer(layer, true);
2020-11-04 16:39:20 +00:00
// Subscribe to Loosing connection:
2020-11-23 06:09:31 +00:00
client.on("disconnect", () => {
_this._logger.info("Connection of : " + client.id + " lost.");
2020-11-04 16:39:20 +00:00
_this._sockets.delete(client);
2021-03-19 18:17:39 +00:00
// Manually Remove the Layer
_this.removeLayer(layer);
2020-11-23 06:09:31 +00:00
if (_this._sockets.size === 0) {
_this.connected.setContent(false);
2020-11-23 06:09:31 +00:00
_this._logger.warn("All Connections lost");
}
2020-11-04 16:39:20 +00:00
});
_this.connected.forcePublish();
2020-11-04 16:39:20 +00:00
});
2020-09-10 16:21:01 +00:00
2020-11-04 16:39:20 +00:00
this._sockets = new Set();
}
2021-03-19 18:17:39 +00:00
async dispose() {
// Dispose the Connection Flag.
this.connected.dispose();
// Dispose every connection.
2021-03-19 18:17:39 +00:00
for (const client of this._sockets) {
client.disconnect();
}
}
}