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
```sh
{
- "packet": "",
- "type: 1
+ [17:21:17] [RECV] stat 16258 16108 4152 7589 0 1458
+ [17:23:26] [SEND] walk 15 15 1 12
}
```
diff --git a/api.ts b/api.ts
index 7b35cb3..594a942 100644
--- a/api.ts
+++ b/api.ts
@@ -1,5 +1,4 @@
import * as net from "net";
-import { Worker } from "worker_threads";
import config from "./config.json";
enum Type {
@@ -30,7 +29,7 @@ class PhoenixAPI {
private _socket: net.Socket;
private _do_work: boolean;
private _messages: string[];
- private _worker: Worker;
+ public data: string = "";
constructor(port: number) {
this._socket = new net.Socket();
@@ -38,10 +37,26 @@ class PhoenixAPI {
this._do_work = true;
this._messages = [];
- this._worker = new Worker("./worker.js", { workerData: null });
- this._worker.on("message", this._handleMessage);
- this._worker.on("error", this._handleError);
- this._worker.on("exit", this._handleExit);
+
+ this._socket.on("data", (buffer) => {
+ 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");
-});