Skip to main content
Module

x/minecraft_lib/network/mod.ts>Connection

A collection of modules that can be used to build Minecraft servers, clients, utilities and other tools.
Latest
class Connection
import { Connection } from "https://deno.land/x/minecraft_lib@0.1.0/network/mod.ts";

Represets the client or server end of a connection.

It handles packet framing, compression, encryption, and serialization of packets when a protocol is specified.

Constructors

new
Connection(conn: Deno.Conn)

Properties

readonly
closed

Methods

Closes the underlying Deno.Conn and calls PacketHandler.onDisconnect on the packet handler, if specified with the protocol.

receive(): Promise<Packet | null>

Receives and deserializes a packet using the previously specified protocol.

When a packet handler is specified with the protocol, the Packet.handle method will be called.

If no protocol is set, this method will throw an exception.

Once there are no more packets to read, e.g. because the connection has been closed, this method returns a null value.

receive<P extends Packet>(constructor: PacketConstructor<P>): Promise<P | null>
receiveRaw(): Promise<Uint8Array | null>

Receives the raw packet data, without deserializing it.

send(packet: Packet): Promise<void>

Sends a packet and serializes it using the previously specified protocol.

If no protocol is set, this method will throw an exception.

sendRaw(buf: Uint8Array): Promise<void>

Sends raw packet data.

setClientProtocol<Handler extends PacketHandler | void>(protocol: Protocol<PacketHandler | void, Handler>, handler?: Handler)

Sets the server protocol, optionally specifying a packet handler.

setCompressionThreshold(threshold: number)

Sets the compression threshold value. Negative values disable compression.

If the value is set to a non-negative number, the packet format is changed and all packets larger than the threshold in bytes are compressed.

setEncryption(key: Uint8Array)

Enables encryption on the connection and initializes the cipher with the specified key.

setServerProtocol<Handler extends PacketHandler | void>(protocol: Protocol<Handler, PacketHandler | void>, handler?: Handler)

Sets the client protocol, optionally specifying a packet handler.