Fixing IO-Server
This commit is contained in:
parent
b3a6bfea5a
commit
92d33960d8
@ -2,7 +2,7 @@
|
||||
* @author Martin Karkowski
|
||||
* @email m.karkowski@zema.de
|
||||
* @create date 2021-03-22 19:03:15
|
||||
* @modify date 2021-04-09 08:34:22
|
||||
* @modify date 2021-04-13 15:35:32
|
||||
* @desc [description]
|
||||
*/
|
||||
|
||||
@ -33,7 +33,6 @@ const EVENTS_TO_FORWARD: Set<string> = new Set([
|
||||
* @class IoSocketMirrorServer
|
||||
*/
|
||||
export class IoSocketMirrorServer extends EventMirror {
|
||||
protected _socket: io.Server;
|
||||
protected _sockets: Set<io.Socket>;
|
||||
|
||||
/**
|
||||
@ -46,7 +45,7 @@ export class IoSocketMirrorServer extends EventMirror {
|
||||
super(
|
||||
// As event Emitter, we provide the IO-Client.
|
||||
(io as any)(),
|
||||
getNopeLogger("io-mirror-client", level)
|
||||
getNopeLogger("io-mirror-server", level)
|
||||
);
|
||||
|
||||
// Tell the Server to listen.
|
||||
@ -56,11 +55,11 @@ export class IoSocketMirrorServer extends EventMirror {
|
||||
// it will only be true, if a connection with this server has been established
|
||||
this.connected.setContent(false);
|
||||
|
||||
this._logger.info("connecting to: " + uri);
|
||||
this._logger.info("Hosting Server on Port " + port.toString());
|
||||
|
||||
const _this = this;
|
||||
|
||||
this._socket.on("connection", (client) => {
|
||||
(this._emitter as any).on("connection", (client) => {
|
||||
_this._logger.info("New Connection established: " + client.id);
|
||||
|
||||
_this._sockets.add(client);
|
||||
|
@ -2,11 +2,11 @@
|
||||
* @author Martin Karkowski
|
||||
* @email m.karkowski@zema.de
|
||||
* @create date 2021-03-22 13:53:54
|
||||
* @modify date 2021-03-22 13:53:54
|
||||
* @modify date 2021-04-13 15:29:25
|
||||
* @desc [description]
|
||||
*/
|
||||
|
||||
import { IoSocketMirror } from "../../../lib/communication/mirrors/ioSocketMirror";
|
||||
import { IoSocketMirrorClient } from "../../../lib/communication/mirrors/ioSocketMirrorClient";
|
||||
import { exportProperty } from "../../../lib/decorators/moduleDecorators";
|
||||
import { getNopeLogger } from "../../../lib/logger/getLogger";
|
||||
import { LoggerLevel } from "../../../lib/logger/nopeLogger";
|
||||
@ -67,7 +67,7 @@ export class MirrorLinkModule extends InjectableNopeBaseModule {
|
||||
// Make shure we use the http before connecting.
|
||||
const uriToUse = uri.startsWith("http://") ? uri : "http://" + uri;
|
||||
|
||||
this._layer = new IoSocketMirror(uriToUse, level);
|
||||
this._layer = new IoSocketMirrorClient(uriToUse, level);
|
||||
|
||||
// Forward the Connection.
|
||||
this.connected.setContent(false);
|
||||
@ -93,12 +93,7 @@ export class MirrorLinkModule extends InjectableNopeBaseModule {
|
||||
protected _id: string;
|
||||
|
||||
public async dispose() {
|
||||
this._dispatcher.communicator.removeLayer(this._layer);
|
||||
|
||||
if (this._layer) {
|
||||
await this._layer.dispose();
|
||||
}
|
||||
|
||||
this._dispatcher.communicator.removeMirror(this._layer);
|
||||
await super.dispose();
|
||||
}
|
||||
}
|
||||
|
@ -2,55 +2,19 @@
|
||||
* @author Martin Karkowski
|
||||
* @email m.karkowski@zema.de
|
||||
* @create date 2021-03-22 18:06:08
|
||||
* @modify date 2021-03-22 18:06:08
|
||||
* @modify date 2021-04-12 14:22:18
|
||||
* @desc [description]
|
||||
*/
|
||||
import * as io from "socket.io";
|
||||
import { exportProperty } from "../../../lib/decorators/moduleDecorators";
|
||||
import { getNopeLogger } from "../../../lib/logger/getLogger";
|
||||
import { IoSocketMirrorServer } from "../../../lib/communication/mirrors/ioSocketMirrorServer";
|
||||
import { LoggerLevel } from "../../../lib/logger/nopeLogger";
|
||||
import { InjectableNopeBaseModule } from "../../../lib/module/BaseModule.injectable";
|
||||
import { NopeObservable } from "../../../lib/observables/nopeObservable";
|
||||
|
||||
const EVENTS_TO_FORWARD: Set<string> = new Set([
|
||||
// Default emitters
|
||||
"aurevoir",
|
||||
"bonjour",
|
||||
"NewInstanceGeneratorsAvailable",
|
||||
"NewInstancesAvailable",
|
||||
"NewObersvablesAvailable",
|
||||
"NewServicesAvailable",
|
||||
"StatusUpdate",
|
||||
"TaskCancelation",
|
||||
"event",
|
||||
"rpcRequest",
|
||||
"rpcResponse"
|
||||
]);
|
||||
|
||||
export class MirrorServerModule extends InjectableNopeBaseModule {
|
||||
/**
|
||||
* Attribute holding a Flag, whether the Element is connected or not.
|
||||
*
|
||||
* @memberof MirrorServerModule
|
||||
*/
|
||||
@exportProperty({
|
||||
mode: "publish",
|
||||
schema: {
|
||||
description: "An Overview, showing the connected clients",
|
||||
type: "array",
|
||||
items: {
|
||||
description: "The ID of the connection.",
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
topic: "clients"
|
||||
})
|
||||
public clients = new NopeObservable<string[]>();
|
||||
|
||||
protected _logger = getNopeLogger("Mirror-Server");
|
||||
protected _socket: io.Server;
|
||||
protected _sockets: Set<io.Socket>;
|
||||
|
||||
async init(port = 7001): Promise<void> {
|
||||
async init(
|
||||
layer = "io-server",
|
||||
port = 7001,
|
||||
level: LoggerLevel = "debug"
|
||||
): Promise<void> {
|
||||
const _this = this;
|
||||
|
||||
// Define the Author.
|
||||
@ -68,68 +32,13 @@ export class MirrorServerModule extends InjectableNopeBaseModule {
|
||||
|
||||
await super.init();
|
||||
|
||||
this.clients.setContent([]);
|
||||
|
||||
this._logger.info(
|
||||
"waiting for connection. Listening on port: " + port.toString()
|
||||
);
|
||||
|
||||
this._socket = (io as any)();
|
||||
this._socket.listen(port);
|
||||
|
||||
this._socket.on("connection", (client) => {
|
||||
_this._logger.info("New Connection established: " + client.id);
|
||||
|
||||
_this._sockets.add(client);
|
||||
|
||||
// Now Subscribe the Events and make shure we
|
||||
// are forwarding the data.
|
||||
for (const event of EVENTS_TO_FORWARD) {
|
||||
client.on(event, (data) => {
|
||||
_this._forward(client, event, data);
|
||||
});
|
||||
}
|
||||
|
||||
// Subscribe to Loosing connection:
|
||||
client.on("disconnect", () => {
|
||||
_this._logger.info("Connection of : " + client.id + " lost.");
|
||||
_this._sockets.delete(client);
|
||||
_this.clients.forcePublish();
|
||||
});
|
||||
|
||||
_this.clients.forcePublish();
|
||||
});
|
||||
|
||||
this._sockets = new Set();
|
||||
}
|
||||
|
||||
protected _id: string;
|
||||
|
||||
/**
|
||||
* 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 MirrorServerModule
|
||||
*/
|
||||
protected _forward(socket: io.Socket, event: string, data: any): void {
|
||||
for (const socketToForward of this._sockets) {
|
||||
if (socket !== socketToForward) {
|
||||
socketToForward.emit(event, data);
|
||||
}
|
||||
switch (layer) {
|
||||
case "io-server":
|
||||
this._dispatcher.communicator.addMirror(
|
||||
new IoSocketMirrorServer(port, level),
|
||||
false
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposing the Module, results in closing all connections.
|
||||
*
|
||||
* @return {*} {Promise<void>}
|
||||
* @memberof MirrorServerModule
|
||||
*/
|
||||
public async dispose(): Promise<void> {
|
||||
this._socket.close();
|
||||
await super.dispose();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user