Skip to content

Commit

Permalink
🐛 fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
XiYang6666 committed Apr 15, 2024
1 parent 241559f commit 568b638
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions example/ping_mc_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createConnection } from "net";
import { createConnection, Socket } from "net";
import { StructBuilder, BaseTypes, write, Package, read } from "../src/";
import { Socket } from "net";
import { format } from "util";

const enum State {
HANDSHAKE = 0,
Expand Down Expand Up @@ -38,8 +38,9 @@ function createCasedPacketBuffer(packetId: number, data: Buffer): Buffer {
}

function readCasedPacket(buffer: Buffer): [casedPacket | undefined, number] {
if (buffer.length === 0) return [undefined, 0];
const [length, lengthOffset] = read(BaseTypes.VarInt32, buffer, 0);
if (length < buffer.length) {
if (length > buffer.length) {
return [undefined, 0];
}
const [packetId, idOffset] = read(BaseTypes.VarInt32, buffer, lengthOffset);
Expand All @@ -52,7 +53,7 @@ function sendMcPacket(client: Socket, packetId: number, packet: Package<any>) {
client.write(createCasedPacketBuffer(packetId, packet.buffer));
}

const host = "mc.xasmc.xyz";
const host = "2b2t.xin";
const port = 25565;
let state: State = State.HANDSHAKE;

Expand All @@ -62,6 +63,8 @@ const client = createConnection({ host, port }, () => {
let tempBuffer = Buffer.alloc(0);

client.on("data", (data) => {
console.log("awa");
console.log(data);
tempBuffer = Buffer.concat([tempBuffer, data]);

while (true) {
Expand All @@ -88,6 +91,14 @@ const client = createConnection({ host, port }, () => {
}
});

client.on("error", (err) => {
console.log(err);
});

client.on("end", () => {
console.log("end");
});

// 握手 切换到 Status 状态
sendMcPacket(
client,
Expand Down

0 comments on commit 568b638

Please sign in to comment.