From 2591e2cbfb1082586f8688e3ea6d2666138974d6 Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 11 Nov 2020 18:24:21 +0100 Subject: [PATCH] fixing io.sockets and starter to wait for the dispatcher to be ready --- lib/cli/runNopeBackend.ts | 5 +---- lib/communication/IoSocketClient.ts | 16 ++++++++++++++-- lib/communication/IoSocketServer.ts | 4 +++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/cli/runNopeBackend.ts b/lib/cli/runNopeBackend.ts index 486aa57..72e5692 100644 --- a/lib/cli/runNopeBackend.ts +++ b/lib/cli/runNopeBackend.ts @@ -42,7 +42,7 @@ const main = async function () { 'event': [], 'amqp': ['localhost'], 'io-server': [7000], - 'io-client': ['localhost:7000'] + 'io-client': ['http://localhost:7000'] } let opts: { @@ -126,9 +126,6 @@ const main = async function () { await dispatcher.ready.waitFor(value => value); - const sleep = promisify(setTimeout); - await sleep(1000); - // Try to load the Modules. try { await loadPackages(getPackageLoader(dispatcher), args.file) diff --git a/lib/communication/IoSocketClient.ts b/lib/communication/IoSocketClient.ts index da9d314..f151719 100644 --- a/lib/communication/IoSocketClient.ts +++ b/lib/communication/IoSocketClient.ts @@ -7,16 +7,18 @@ */ import { connect, Socket } from 'socket.io-client'; -import { getCentralNopeLogger } from "../logger/getLogger"; +import { getCentralNopeLogger, getNopeLogger } from "../logger/getLogger"; import { NopeObservable } from '../observables/nopeObservable'; import { IAvailableInstanceGeneratorsMsg, IAvailableServicesMsg, IAvailableTopicsMsg, ICommunicationInterface, IExternalEventMsg, IRequestTaskMsg, IResponseTaskMsg, ITaskCancelationMsg } from "../types/nope/nopeCommunication.interface"; import { INopeObservable } from '../types/nope/nopeObservable.interface'; +import { Logger } from "winston"; export class IoSocketClient implements ICommunicationInterface { connected: INopeObservable; protected _emitter: typeof Socket; + protected _logger: Logger; constructor(public uri: string) { const _this = this; @@ -24,15 +26,25 @@ export class IoSocketClient implements ICommunicationInterface { this.connected = new NopeObservable(); this.connected.setContent(false); + // Make shure we use the http before connecting. + this.uri = this.uri.startsWith('http://') ? this.uri : 'http://' + this.uri; + + // Create a Logger + this._logger = getNopeLogger('io-socket-client'); + this._logger.info('connecting to: ' + uri); + this._emitter = connect(uri); + this._emitter.on('connect', (...args) => { // Element is connected + _this._logger.info('connected'); _this.connected.setContent(true); }); this._emitter.on('disconnect', () => { // Connection Lost. + _this._logger.error('Connection lost!'); _this.connected.setContent(false); - }) + }); } async onTaskCancelation(cb: (msg: ITaskCancelationMsg) => void) { diff --git a/lib/communication/IoSocketServer.ts b/lib/communication/IoSocketServer.ts index 293c4d0..0b744ec 100644 --- a/lib/communication/IoSocketServer.ts +++ b/lib/communication/IoSocketServer.ts @@ -41,7 +41,8 @@ export class IoSocketSeverEventEmitter { this.connected = new NopeObservable(); this.connected.setContent(false); - this._logger = getNopeLogger('io-socket'); + this._logger = getNopeLogger('io-socket-server'); + this._logger.info('waiting for connection. Listening on port: ' + port.toString()); this._socket.listen(port); @@ -60,6 +61,7 @@ export class IoSocketSeverEventEmitter { if (_this._sockets.size === 0){ _this.connected.setContent(false); + _this._logger.warn('All Connections lost'); } });