diff --git a/server/src/core/http-server/server.js b/server/src/core/http-server/server.js index 5ee3c73f..15a55566 100644 --- a/server/src/core/http-server/server.js +++ b/server/src/core/http-server/server.js @@ -29,6 +29,7 @@ import infoPlugin from '@/core/http-server/api/info' import downloadsPlugin from '@/core/http-server/api/downloads' import { LogHelper } from '@/helpers/log-helper' import { DateHelper } from '@/helpers/date-helper' +import { TCP_CLIENT } from '@/core/tcp-client' const server = {} @@ -216,10 +217,10 @@ server.handleOnConnection = (socket) => { const provider = await addProvider(socket.id) // Check whether the TCP client is connected to the TCP server - if (global.tcpClient.isConnected) { + if (TCP_CLIENT.isConnected) { socket.emit('ready') } else { - global.tcpClient.ee.on('connected', () => { + TCP_CLIENT.ee.on('connected', () => { socket.emit('ready') }) } diff --git a/server/src/core/ner.js b/server/src/core/ner.js index 7f846c0a..0dd89c12 100644 --- a/server/src/core/ner.js +++ b/server/src/core/ner.js @@ -7,6 +7,7 @@ import fs from 'node:fs' import { LogHelper } from '@/helpers/log-helper' import { StringHelper } from '@/helpers/string-helper' +import { TCP_CLIENT } from '@/core/tcp-client' class Ner { constructor(ner) { @@ -99,13 +100,10 @@ class Ner { resolve(spacyEntities) } - global.tcpClient.ee.removeAllListeners() - global.tcpClient.ee.on( - 'spacy-entities-received', - spacyEntitiesReceivedHandler - ) + TCP_CLIENT.ee.removeAllListeners() + TCP_CLIENT.ee.on('spacy-entities-received', spacyEntitiesReceivedHandler) - global.tcpClient.emit('get-spacy-entities', utterance) + TCP_CLIENT.emit('get-spacy-entities', utterance) }) } diff --git a/server/src/core/nlu.js b/server/src/core/nlu.js index b9191144..690ac80b 100644 --- a/server/src/core/nlu.js +++ b/server/src/core/nlu.js @@ -11,18 +11,12 @@ import kill from 'tree-kill' import { langs } from '@@/core/langs.json' import { version } from '@@/package.json' -import { - HAS_LOGGER, - IS_TESTING_ENV, - TCP_SERVER_BIN_PATH, - TCP_SERVER_HOST, - TCP_SERVER_PORT -} from '@/constants' +import { HAS_LOGGER, IS_TESTING_ENV, TCP_SERVER_BIN_PATH } from '@/constants' import Ner from '@/core/ner' import { LogHelper } from '@/helpers/log-helper' import { StringHelper } from '@/helpers/string-helper' import { LangHelper } from '@/helpers/lang-helper' -import TCPClient from '@/core/tcp-client' +import { TCP_CLIENT } from '@/core/tcp-client' import Conversation from '@/core/conversation' const defaultNluResultObj = { @@ -227,10 +221,9 @@ class Nlu { shell: true }) - global.tcpClient = new TCPClient(TCP_SERVER_HOST, TCP_SERVER_PORT) - - global.tcpClient.ee.removeListener('connected', connectedHandler) - global.tcpClient.ee.on('connected', connectedHandler) + TCP_CLIENT.init() + TCP_CLIENT.ee.removeListener('connected', connectedHandler) + TCP_CLIENT.ee.on('connected', connectedHandler) }) return {} diff --git a/server/src/core/tcp-client.ts b/server/src/core/tcp-client.ts index 07e3c0dc..2435f243 100644 --- a/server/src/core/tcp-client.ts +++ b/server/src/core/tcp-client.ts @@ -1,7 +1,11 @@ import Net from 'node:net' import { EventEmitter } from 'node:events' -import { IS_PRODUCTION_ENV } from '@/constants' +import { + IS_PRODUCTION_ENV, + TCP_SERVER_HOST, + TCP_SERVER_PORT +} from '@/constants' import { LogHelper } from '@/helpers/log-helper' import { OSHelper, OSTypes } from '@/helpers/os-helper' @@ -10,22 +14,30 @@ const INTERVAL = IS_PRODUCTION_ENV ? 3000 : 500 // Number of retries to connect to the TCP server const RETRIES_NB = IS_PRODUCTION_ENV ? 8 : 30 -export default class TCPClient { +class TCPClient { + private static instance: TCPClient + private readonly host: string private readonly port: number + private reconnectCounter = 0 private tcpSocket = new Net.Socket() + public ee = new EventEmitter() public status = this.tcpSocket.readyState public isConnected = false constructor(host: string, port: number) { + if (!TCPClient.instance) { + LogHelper.title('TCP Client') + LogHelper.success('New instance') + + TCPClient.instance = this + } + this.host = host this.port = port - LogHelper.title('TCP Client') - LogHelper.success('New instance') - this.tcpSocket.on('connect', () => { LogHelper.title('TCP Client') LogHelper.success( @@ -81,7 +93,9 @@ export default class TCPClient { LogHelper.title('TCP Client') LogHelper.success('Disconnected from the TCP server') }) + } + public init(): void { setTimeout(() => { this.connect() }, INTERVAL) @@ -103,3 +117,8 @@ export default class TCPClient { }) } } + +export const TCP_CLIENT = new TCPClient( + String(TCP_SERVER_HOST), + TCP_SERVER_PORT +) diff --git a/server/src/helpers/loader-helper.ts b/server/src/helpers/loader-helper.ts index 1df9742a..467bcf79 100644 --- a/server/src/helpers/loader-helper.ts +++ b/server/src/helpers/loader-helper.ts @@ -18,7 +18,6 @@ export class LoaderHelper { private static spinner = new Spinner('\x1b[95m%s\x1b[0m\r').setSpinnerString( 18 ) - private static interval: NodeJS.Timer | undefined /** diff --git a/server/src/index.ts b/server/src/index.ts index ed9ac5d0..ba129337 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,14 +1,12 @@ import { spawn } from 'node:child_process' import { - TCP_SERVER_HOST, - TCP_SERVER_PORT, IS_DEVELOPMENT_ENV, LANG as LEON_LANG, TCP_SERVER_BIN_PATH } from '@/constants' import { LangHelper } from '@/helpers/lang-helper' -import TCPClient from '@/core/tcp-client' +import { TCP_CLIENT } from '@/core/tcp-client' import server from '@/core/http-server/server' ;(async (): Promise => { process.title = 'leon' @@ -23,7 +21,7 @@ import server from '@/core/http-server/server' ) // Start the TCP client - global.tcpClient = new TCPClient(String(TCP_SERVER_HOST), TCP_SERVER_PORT) + TCP_CLIENT.init() // Start the core server await server.init() diff --git a/skills/leon/good_bye/config/fr.json b/skills/leon/good_bye/config/fr.json index a4e3345e..8654cb2f 100644 --- a/skills/leon/good_bye/config/fr.json +++ b/skills/leon/good_bye/config/fr.json @@ -16,7 +16,7 @@ "Au revoir ! Prenez soin de vous.", "Au revoir, merci de prendre soin de vous-même.", "Bye ! Profitez de votre temps à bon escient !", - "A la prochaine !" + "À la prochaine !" ] } }