nope/lib/communication/IoSocketServer.ts

94 lines
2.5 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
* @modify date 2021-02-05 09:49:24
2020-11-06 08:10:30 +00:00
* @desc [description]
*/
import { EventEmitter } from "events";
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";
import { getNopeLogger } from "../logger/getLogger";
import { Bridge } from "./bridge";
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
* @param {Server} [server] A Server shich should be used. (Otherwise a Server is created)
* @memberof IoSocketServer
*/
constructor(public port: number, server?: Server) {
2020-12-05 01:28:33 +00:00
super("individual","individual",true, getNopeLogger("io-socket-server", "info"));
2020-11-04 16:39:20 +00:00
if (server) {
this._socket = (io as any)(server);
} else {
this._socket = (io as any)();
}
const _this = this;
this.connected.setContent(false);
// Add a default Event-Layer
// Perhaps that can be removed.
2020-12-05 01:28:33 +00:00
this.addLayer(new EventLayer(new EventEmitter(), "individual","individual",getNopeLogger("io-socket-server-internal")),false);
this._logger.info(
"waiting for connection. Listening on port: " + port.toString()
);
2020-11-04 16:39:20 +00:00
this._socket.listen(port);
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
// Add the Elements to the Client
_this._sockets.add(client);
// Add the socket as new layer:
const _layer = new EventLayer(client, "individual","individual");
2020-12-05 01:28:33 +00:00
_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);
_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();
}
async dispose(){
// Dispose the Connection Flag.
this.connected.dispose();
// Dispose every connection.
for (const client of this._sockets){
client.disconnect();
}
}
}