From 8d92c94fff889c32d44b907010f4783436a209eb Mon Sep 17 00:00:00 2001 From: 0xPierre Date: Sat, 8 Jul 2023 03:25:29 +0200 Subject: [PATCH] remove worker & improve console log --- README.md | 10 +++------- api.ts | 41 +++++++++++++++++++++-------------------- index.ts | 18 +++++++++++++++++- worker.js | 32 -------------------------------- 4 files changed, 41 insertions(+), 60 deletions(-) delete mode 100644 worker.js diff --git a/README.md b/README.md index 211fb49..a026061 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,7 @@

Developed by 0xPierre -

- - +

## Getting Started @@ -37,8 +35,6 @@ You must have Nodejs - ## Usage 1. Start API @@ -49,7 +45,7 @@ You must have Nodejs { + this.data += buffer.toString(); + let delim_pos = this.data.indexOf("\x01"); + + while (delim_pos !== -1) { + const msg = this.data.substring(0, delim_pos); + this.data = this.data.substring(delim_pos + 1); + this._messages.push(msg); + delim_pos = this.data.indexOf("\x01"); + } + }); + + this._socket.on("error", (err) => { + console.error("Socket error:", err); + }); + + this._socket.on("close", () => { + console.log("Socket closed"); + }); } private _sendData(data: string): number { @@ -60,23 +75,10 @@ class PhoenixAPI { return this._do_work; } - private _handleMessage = (msg: any): void => { - this._messages.push(msg); - }; - - private _handleError = (err: Error): void => { - console.error("Worker error:", err); - }; - - private _handleExit = (code: number): void => { - console.log("Worker exited with code", code); - }; - public close(): void { if (this.isWorkerRunning()) { this._do_work = false; this._socket.destroy(); - this._worker.terminate(); } } @@ -84,7 +86,6 @@ class PhoenixAPI { if (this._messages.length === 0) { return ""; } - return this._messages.shift()!; } diff --git a/index.ts b/index.ts index 43d322a..9185510 100644 --- a/index.ts +++ b/index.ts @@ -3,12 +3,28 @@ import config from "./config.json"; const api = new PhoenixAPI(Number(config.port)); +function formatTime(date: Date): string { + var hours = date.getHours().toString().padStart(2, "0"); + var minutes = date.getMinutes().toString().padStart(2, "0"); + var seconds = date.getSeconds().toString().padStart(2, "0"); + + return "[" + hours + ":" + minutes + ":" + seconds + "]"; +} + // Example of receiving messages function receiveMessages() { while (!api.empty()) { const message = api.get_message(); - console.log(message); + + if (message) { + const parseMessage = JSON.parse(message); + if (parseMessage.type === 1) { + console.log(`${formatTime(new Date())} [RECV] ${parseMessage.packet}`); + } else if (parseMessage.type === 0) { + console.log(`${formatTime(new Date())} [SEND] ${parseMessage.packet}`); + } + } } setTimeout(receiveMessages, 100); diff --git a/worker.js b/worker.js deleted file mode 100644 index 5afad72..0000000 --- a/worker.js +++ /dev/null @@ -1,32 +0,0 @@ -const net = require("net"); -const { parentPort } = require("worker_threads"); -const config = require("./config.json"); - -const HOST = String(config.host); -const PORT = Number(config.port); -let data = ""; - -const socket = new net.Socket(); -socket.connect({ host: HOST, port: PORT }); - -socket.on("data", (buffer) => { - data += buffer.toString(); - let delim_pos = data.indexOf("\x01"); - - while (delim_pos !== -1) { - const msg = data.substring(0, delim_pos); - data = data.substring(delim_pos + 1); - - parentPort.postMessage(msg); - - delim_pos = data.indexOf("\x01"); - } -}); - -socket.on("error", (err) => { - console.error("Socket error:", err); -}); - -socket.on("close", () => { - console.log("Socket closed"); -});