Search docs
Search guides, modules and symbols
Modules

Docs / API / Modules / net

Sockets

Network

@s2script/sdk/net

Raw TCP / UDP sockets. TcpSocket.send and UdpSocket.sendTo return boolean admission — false means closed, oversized, or full; true is queued, not delivered.

Import

import { TcpSocket } from "@s2script/sdk/net";

Author-time types ship in the npm package; the engine injects the runtime at plugin load. Add @s2script/sdk to your plugin's dependencies.

API reference

Generated from the shipped type definitions.

interface
TcpSocket

A connected TCP client socket — a per-plugin handle over a copied byte stream (no live socket crosses to JS).

send(data: Uint8Array | string): boolean
— Send bytes; a string is sent as its UTF-8 encoding. Returns false on closed/oversized/full admission.
onData(handler: (bytes: Uint8Array) => void): void
— Register a handler invoked for each inbound chunk of bytes.
onClose(handler: () => void): void
— Register a handler for the peer closing the connection.
onError(handler: (err: string) => void): void
— Register a handler for a transport error; err is the error text.
close(): void
— Close the connection.
interface
UdpSocket

A bound UDP socket — a per-plugin handle for connectionless datagrams.

sendTo(host: string, port: number, data: Uint8Array | string): boolean
— Send a datagram to host:port; a string is sent as its UTF-8 encoding. Returns false on closed/oversized/full admission.
onMessage(handler: (from: { host: string; port: number }, bytes: Uint8Array) => void): void
— Register a handler invoked for each inbound datagram; from is the sender's address.
close(): void
— Close the socket.
const
Net

Entry point for opening raw TCP + UDP sockets.

connectTcp(host: string, port: number): Promise<TcpSocket>

Connect a TCP client to host:port off the game thread.

Returns — Resolves on connect with the live TcpSocket handle.

Throws — Rejects on connect failure (refused, unreachable, DNS error).

import { Net } from "@s2script/sdk/net";
const sock = await Net.connectTcp("127.0.0.1", 9000);
sock.onData((bytes) => console.log("got " + bytes.length + " bytes"));
sock.send("hello");
udp(): Promise<UdpSocket>

Bind a UDP socket on an ephemeral local port.

Returns — Resolves with the bound UdpSocket handle.

Back to modules All packages