Modules

Docs / API / Modules / net

Sockets

Network

@s2script/sdk/net

engine-generic raw TCP + UDP sockets (binary), off the game thread.

3 exports

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): void
Send bytes; a string is sent as its UTF-8 encoding.
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): void
Send a datagram to host:port; a string is sent as its UTF-8 encoding.
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.

ReturnsResolves on connect with the live TcpSocket handle.

ThrowsRejects 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.

ReturnsResolves with the bound UdpSocket handle.

Back to modules All packages

s2script — Source 2 plugin framework

GitHub