Client Module
Minecraft socket pipeline utilities. Support Minecraft lan server discovery.
Usage
Ping Minecraft Server
Read sever info (server ip, port) and fetch its status (ping, server motd):
import { queryStatus, Status, QueryOptions } from '@xmcl/client'
const serverInfo = {
host: 'your host',
port: 25565, // be default
};
const options: QueryOptions = {
/**
* see http://wiki.vg/Protocol_version_numbers
*/
protocol: 203,
};
const rawStatusJson: Status = await fetchStatus(info, options);
Detect LAN Minecraft Server
You can detect if player share LAN server.
Or you can fake a LAN server.
import { MinecraftLanDiscover, LanServerInfo } from '@xmcl/client'
const discover = new MinecraftLanDiscover();
await discover.bind(); // start to listen any lan server
discover.on('discover', ({ motd, port }: LanServerInfo) => {
console.log(motd); // server motd
console.log(port); // server port
})
const isReady = discover.isReady // a boolean represent whether the discover is ready to use
// you can also fake a lan server
discover.broadcast({
motd: 'your motd',
port: 2384 // fake port
});
// fake LAN server is useful when you want to implement the P2P connection between two players
dicover.destroy(); // stop listening
🧾 Classes
🤝 Interfaces
🏭 Functions
Field
Field(type: Coder<T>): Function
Annotate the field type in your packet. Assign a coder for serialization/deserialization. This will generate a list of FieldType
in your class prototype.
Parameters
- type:
Coder<T>
The coder to serialize/deserialize the field.
Return Type
Function
Defined in: packages/client/packet.ts:37
Packet
Packet(side: Side, id: number, state: keyof States, name: string= ''): Function
Decoarte for you packet class. This will generate a PacketRegistryEntry
in your class prototype.
Parameters
- side:
Side
The side of your packet - id:
number
The id of your packet - state:
keyof States
The state of you packet should be - name:
string
Return Type
Function
Defined in: packages/client/packet.ts:54
createChannel
createChannel(): Channel
Create a channel with Handleshake, ServerQuery, ServerStatus, Ping, Pong packets are registered.
This is a lower level function for the case that you want to use channel directly
Return Type
Channel
Defined in: packages/client/status.ts:109
createClient
createClient(protocol: number, timeout: number): Object
Create a query client for certain protocol and timeout setting.
Parameters
- protocol:
number
The protocol number - timeout:
number
The timeout millisecond
Return Type
Object
Defined in: packages/client/status.ts:156
getPacketRegistryEntry
getPacketRegistryEntry(clazz: Function): PacketRegistryEntry
Get a packet registry entry for a class
Parameters
- clazz:
Function
The class object
Return Type
PacketRegistryEntry
Defined in: packages/client/packet.ts:26
queryStatus
queryStatus(server: Object, options: QueryOptions= {}): Promise<Status>
Query the server status in raw JSON format in one shot.
Parameters
- server:
Object
The server information - options:
QueryOptions
The query options
Return Type
Promise<Status>
Defined in: packages/client/status.ts:125
🏷️ Variables
Bool
Bool: Coder<boolean> = ...
Defined in: packages/client/coders.ts:42
Byte
Byte: Coder<number> = ...
Defined in: packages/client/coders.ts:32
ByteArray
ByteArray: Coder<Int8Array> = ...
Defined in: packages/client/coders.ts:176
Double
Double: Coder<number> = ...
Defined in: packages/client/coders.ts:52
Float
Float: Coder<number> = ...
Defined in: packages/client/coders.ts:47
Int
Int: Coder<number> = ...
Defined in: packages/client/coders.ts:27
Json
Json: Coder<any> = ...
Defined in: packages/client/coders.ts:134
LAN_MULTICAST_ADDR
LAN_MULTICAST_ADDR: "224.0.2.60" = '224.0.2.60'
Defined in: packages/client/lan.ts:4
LAN_MULTICAST_ADDR_V6
LAN_MULTICAST_ADDR_V6: "FF75:230::60" = 'FF75:230::60'
Defined in: packages/client/lan.ts:5
LAN_MULTICAST_PORT
LAN_MULTICAST_PORT: 4445 = 4445
Defined in: packages/client/lan.ts:6
Long
Long: Coder<bigint> = ...
Defined in: packages/client/coders.ts:112
PacketFieldsMetadata
PacketFieldsMetadata: typeof PacketFieldsMetadata = ...
Defined in: packages/client/packet.ts:20
PacketMetadata
PacketMetadata: typeof PacketMetadata = ...
Defined in: packages/client/packet.ts:19
Short
Short: Coder<number> = ...
Defined in: packages/client/coders.ts:102
Slot
Slot: Coder<SlotData> = ...
Defined in: packages/client/coders.ts:143
String
String: Coder<string> = ...
Defined in: packages/client/coders.ts:122
UByte
UByte: Coder<number> = ...
Defined in: packages/client/coders.ts:37
UShort
UShort: Coder<number> = ...
Defined in: packages/client/coders.ts:107
UUID
UUID: Coder<string> = ...
Defined in: packages/client/coders.ts:57
VarInt
VarInt: Coder<number> = ...
Defined in: packages/client/coders.ts:22
VarLong
VarLong: Coder<bigint> = ...
Defined in: packages/client/coders.ts:117
⏩ Type Aliases
FieldType
FieldType: Function
Defined in: packages/client/packet.ts:16
PacketType
PacketType: Function
Defined in: packages/client/packet.ts:17
Side
Side: "server" | "client"
Defined in: packages/client/packet.ts:6
State
State: keyof States
Defined in: packages/client/channel.ts:9