Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Jun 22, 2023
1 parent 39dd49a commit 5bbc7af
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm i growtopia.js
```js
const { Client, TextPacket, Peer } = require("growtopia.js");

const client = new Client("127.0.0.1", 17091, { https: true });
const client = new Client();

client.on("ready", () => {
console.log(`Starting ENet server ${client.config.port} on ${client.config.ip}`);
Expand All @@ -36,32 +36,35 @@ client.on("error", (err) => {
client.on("connect", (netID) => {
console.log(`Connected netID ${netID}`);
const peer = new Peer(client, netID);
peer.send(TextPacket.from(0x1)); // Send Hello Packet
peer.send(TextPacket.from(0x1));
});

client.on("disconnect", (netID) => {
console.log(`Disconnected netID ${netID}`);
});

// Receive Game message packet
client.on("raw", (netID, data) => {
console.log("raw", data);
});

client.on("action", (peer, data) => {
console.log(`Peer (${peer.data.netID}) action`, { data });
if (data.action === "quit") {
peer.disconnect();
}
});

// Receive Tank update packet
client.on("tank", (peer, tank) => {
console.log(`Peer (${peer.data.netID}) tank`, { tank });
});

// Receive Generic text packet
client.on("text", (peer, data) => {
console.log(peer.ping);

console.log(`Peer (${peer.data.netID}) text`, { data });
});

client.listen(1024);
client.listen();
```

## Links
Expand Down
6 changes: 5 additions & 1 deletion lib/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void Client::create(NAPI_CB)
Napi::Env env = info.Env();

uint32_t maxPeers = info[0].As<Napi::Number>().Uint32Value();
bool isClient = info[1].As<Napi::Boolean>().Value();

if (enet_initialize() != 0)
return Napi::Error::New(env, "ENet failed to initialize").ThrowAsJavaScriptException();
Expand All @@ -61,7 +62,10 @@ void Client::create(NAPI_CB)
this->client = enet_host_create(&address, maxPeers, 2, 0, 0);

this->client->checksum = enet_crc32;
this->client->usingNewPacket = usingNewPacket;
if (isClient)
this->client->usingNewPacket = usingNewPacket;
else
this->client->usingNewPacketForServer = usingNewPacket;

enet_host_compress_with_range_coder(this->client);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "growtopia.js",
"version": "1.1.7",
"version": "1.2.0",
"description": "A package to create a growtopia private servers.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
38 changes: 26 additions & 12 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ const Native = require("../../lib/build/Release/index.node").Client;

class Client extends EventEmitter {
public _client: ClientType;
public config: { ip: string; port: number };
public config: ClientOptions;

constructor(ip = "127.0.0.1", port = 17091, public options: ClientOptions) {
constructor(options: ClientOptions) {
super();

this._client = new Native(ip, port) as ClientType;
this.config = {
ip,
port
};
this.options = Object.assign({ https: false }, options);
this.config = Object.assign(
{
https: {
enable: true,
url: "127.0.0.1",
type2: false
},
ip: "127.0.0.1",
port: 17091,
enet: {
maxPeers: 1024,
useNewPacket: {
asClient: false
}
}
},
options
);

this._client = new Native(this.config.ip, this.config.port) as ClientType;
}

public on(event: "connect", listener: (netID: number) => void): this;
Expand Down Expand Up @@ -55,9 +69,9 @@ class Client extends EventEmitter {
return this._client.setEmit(emit);
}

public listen(maxPeers: number) {
public listen() {
try {
this._client.create(maxPeers);
this._client.create(this.config.enet.maxPeers, this.config.enet.useNewPacket.asClient);

this.emitter(this.emit.bind(this));

Expand All @@ -78,8 +92,8 @@ class Client extends EventEmitter {
}

private startWeb() {
if (this.options.https) {
WebServer(this.config.ip, this.config.port);
if (this.config.https.enable) {
WebServer(this.config.ip, this.config.port, this.config.https.type2);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/structures/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const options = {
cert: readFileSync(path.join(__dirname, "..", "..", "misc", "ssl", "server.crt"))
};

export function WebServer(ip: string, port: number) {
export function WebServer(ip: string, port: number, type2 = false) {
const app = express();
let done = false;

Expand All @@ -18,7 +18,9 @@ export function WebServer(ip: string, port: number) {

app.use("/growtopia/server_data.php", (req, res) => {
res.send(
`server|${ip}\nport|${port}\ntype|1\n#maint|Server is Maintenance\nmeta|growtopiajs\nRTENDMARKERBS1001`
`server|${ip}\nport|${port}\ntype|1\n${
type2 ? "type2|1" : ""
}\n#maint|Server is Maintenance\nmeta|growtopiajs\nRTENDMARKERBS1001`
);
});

Expand Down
16 changes: 14 additions & 2 deletions types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface ClientType {
ip: string;
port: number;
setEmit: (emit: (...args: any[]) => void) => void;
create: (maxPeers: number) => void;
create: (maxPeers: number, isClient: boolean) => void;
service: () => void;
deInit: () => void;
send: (peerID: number, count: number, packets: Buffer[]) => void;
Expand All @@ -16,5 +16,17 @@ export interface ClientType {

export interface ClientOptions {
/** Built-in https web server */
https: boolean;
https: {
enable: boolean;
url: string;
type2: boolean;
};
ip: string;
port: number;
enet: {
maxPeers?: number;
useNewPacket?: {
asClient?: boolean;
};
};
}

0 comments on commit 5bbc7af

Please sign in to comment.