Warp/docs/api/1.1/client.md

4.3 KiB

Client

For Client-sided operations.

Getting the Client Object

local Client = Warp.Client()

.awaitReady

Yields the current thread until the client has successfully initialized and synchronized with the server's replication data (identifier).

::: info Its optionally, but highly recommended to call this before firing or connecting to any events to ensure the network is fully ready. :::

::: code-group

() -> ()
local Client = Warp.Client()

-- wait for the client to be fully initialized
Client.awaitReady()

print("Client is now ready to send and receive events! :D")

:::

.Connect

Connect to an event to receive incoming data from the server.

::: code-group

(
	remoteName: string,
	fn: (...any) -> ...any
) -> Connection
local connection = Client.Connect("ServerNotify", function(message, sender)
	print(`Server message from {sender}: {message}`)
end)
print(connection.Connected)

:::

.Once

Similar to :Connect but automatically disconnects after the first firing.

::: code-group

(
	remoteName: string,
	fn: (...any) -> ...any
) -> Connection
Client.Once("WelcomeMessage", function(welcomeText)
	print(`Welcome: {welcomeText}`)
end)

:::

.Wait

Wait for an event to be triggered.

::: code-group

(
	remoteName: string
) -> (number, ...any)
local elapsed, message = Client.Wait("ServerMessage")
print(`Received message after {elapsed} seconds: {message}`)

:::

.Disconnect

Disconnect the event connection.

::: code-group

()
local connection = Client.Connect("ServerNotify", function(message, sender)
	print(`Server message from {sender}: {message}`)
	-- Disconnect the connection
	connection:Disconnect()
end)
print(Connection.Connected)

:::

.DisconnectAll

Disconnect all connections for a specific event.

::: code-group

(
	remoteName: string
)
Client.DisconnectAll("ServerNotify")

:::

.Destroy

Disconnect all connections and remove the event.

::: code-group

(
	remoteName: string
)
Client.Destroy("ServerNotify")

:::

.Fire

Fire an event to the server.

::: code-group

(
	remoteName: string,
	reliable: boolean,
	...: any
)
-- (TCP) Reliable event (guaranteed delivery)
Client.Fire("PlayerAction", true, "jump", playerPosition)

-- (UDP) Unreliable event (faster but not guaranteed)
Client.Fire("PositionUpdate", false, currentPosition)

:::

.Invoke

Invoke the server with timeout support.

::: code-group

(
	remoteName: string,
	timeout: number?,
	...: any
) -> ...any
local Client = Warp.Client()
local response = Client.Invoke("RequestData", 3, "playerStats")
if response then
	print("Server responded:", response)
else
	print("Request timed out")
end

:::

::: warning This function is yielded. Returns nil if timeout occurs. :::

.useSchema

Define a schema for strict data packing on a specific event.

::: code-group

(
	remoteName: string,
	schema: Buffer.SchemaType
)
local Client = Warp.Client()

-- Define a schema for position updates
local positionSchema = Client.Schema.struct({
	x = Client.Schema.f32,
	y = Client.Schema.f32,
	z = Client.Schema.f32,
	timestamp = Client.Schema.u32
})
-- Define a schema for data updates
local dataSchema = Client.Schema.struct({
	Coins = Client.Schema.u32,
	Level = Client.Schema.u8,
	Inventory = Client.Schema.array(Client.Schema.u32),
	Settings = Client.Schema.struct({
		VFX = Client.Schema.boolean,
		Volume = Client.Schema.f32,
		Language = Client.Schema.string,
	})
})

-- Now this event will use the schema
Client.useSchema("DataReplication", dataSchema)
Client.useSchema("PositionUpdate", positionSchema)
Client.Connect("PositionUpdate", function(x, y, z, timestamp)
	-- Data is automatically deserialized according to schema
	updatePlayerPosition(x, y, z)
end)

:::

.Schema

Access to Buffer.Schema for creating data schemas.