Skip to content

Commit

Permalink
remove worker & improve console log
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreCrb committed Jul 8, 2023
1 parent 39f690f commit 8d92c94
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 60 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

<p align="center">
Developed by <a href="https://0xpierre.com/"><strong>0xPierre</strong></a>
</p>

<!-- GETTING STARTED -->
</p>

## Getting Started

Expand Down Expand Up @@ -37,8 +35,6 @@ You must have <a href="https://nodejs.org/en/download"><strong>Nodejs</strong></
}
```

<!-- USAGE EXAMPLES -->

## Usage

1. Start API
Expand All @@ -49,7 +45,7 @@ You must have <a href="https://nodejs.org/en/download"><strong>Nodejs</strong></

```sh
{
"packet": "",
"type: 1
[17:21:17] [RECV] stat 16258 16108 4152 7589 0 1458
[17:23:26] [SEND] walk 15 15 1 12
}
```
41 changes: 21 additions & 20 deletions api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as net from "net";
import { Worker } from "worker_threads";
import config from "./config.json";

enum Type {
Expand Down Expand Up @@ -30,18 +29,34 @@ 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();
this._socket.connect({ host: PhoenixAPI.HOST, port });

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 {
Expand All @@ -60,31 +75,17 @@ 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();
}
}

public get_message(): string {
if (this._messages.length === 0) {
return "";
}

return this._messages.shift()!;
}

Expand Down
18 changes: 17 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 0 additions & 32 deletions worker.js

This file was deleted.

0 comments on commit 8d92c94

Please sign in to comment.