Skip to content

net ​

TCP sockets.

Import

ts
import { TcpSocket, TcpServer, TcpFraming, Net, ... } from "net";

Needs the "net" permission in thicket.json.

TcpSocket class ​

ts
class TcpSocket

A connected TCP socket for bidirectional communication.

TcpSocket.remoteAddress ​

ts
remoteAddress: string

Remote peer address (IP or hostname).

TcpSocket.remotePort ​

ts
remotePort: int

Remote peer port.

TcpSocket.localPort ​

ts
localPort: int

Local bound port.

TcpSocket.write ​

ts
write(data: string | ByteArray): boolean

Write data (string or ByteArray) to the socket. Returns true if the socket accepted the data, false if the write buffer is full and backpressure should be applied.

TcpSocket.close ​

ts
close(): void

Close the socket, terminating the connection.

TcpSocket.onData ​

ts
onData(fn: (data: string) => void): void

Register a callback invoked when text data arrives on the socket. @param fn Callback receiving the data string.

TcpSocket.onBinary ​

ts
onBinary(fn: (data: ByteArray) => void): void

Register a callback invoked when binary data arrives on the socket. @param fn Callback receiving the data as a ByteArray.

TcpSocket.onClose ​

ts
onClose(fn: () => void): void

Register a callback invoked when the socket closes. @param fn Callback with no arguments.

TcpSocket.onError ​

ts
onError(fn: (error: string) => void): void

Register a callback invoked on socket errors. @param fn Callback receiving an error message string.

TcpSocket.setNoDelay ​

ts
setNoDelay(enabled: boolean): void

Enable or disable Nagle's algorithm. Enabled by default; set to false for real-time data.

TcpSocket.setKeepAlive ​

ts
setKeepAlive(enabled: boolean, delaySeconds?: int): void

Enable or disable TCP keepalive. Enabled by default. @param enabled Whether keepalive is active. @param delaySeconds Time in seconds before the first keepalive probe (default 60).

TcpSocket.onPacket ​

ts
onPacket(fn: (packet: ByteArray) => void): void

Register a callback invoked when a framed packet arrives (if framing is enabled on this socket). @param fn Callback receiving the packet data as a ByteArray.

TcpSocket.onDrain ​

ts
onDrain(fn: () => void): void

Register a callback invoked when the write buffer drains (after backpressure clears). @param fn Callback with no arguments.

TcpSocket.writePacket ​

ts
writePacket(data: ByteArray): boolean

Write a framed packet to the socket if framing is enabled. Returns true if accepted, false if backpressured.

TcpServer class ​

ts
class TcpServer

A listening TCP server socket that accepts inbound client connections.

TcpServer.address ​

ts
address: string

Local address the server is bound to.

TcpServer.port ​

ts
port: int

Local port the server is listening on.

TcpServer.onConnection ​

ts
onConnection(fn: (socket: TcpSocket) => void): void

Register a callback invoked each time a client connects. @param fn Callback receiving the connected TcpSocket.

TcpServer.close ​

ts
close(options?: TcpCloseOptions): Promise<void>

Close the server, optionally forcibly terminating active connections. Returns a promise that resolves when all connections are closed or the timeout expires.

TcpServer.connectionCount ​

ts
connectionCount: int

Current number of active client connections.

TcpServer.connections ​

ts
connections: Array<TcpSocket>

Array of active client TcpSocket connections.

TcpServer.broadcast ​

ts
broadcast(data: string | ByteArray): boolean

Broadcast data (string or ByteArray) to all connected clients. Returns true if all accepts succeeded, false if any failed.

TcpFraming class ​

ts
class TcpFraming

Packet framing modes for TCP sockets.

TcpFraming.RAW ​

ts
static RAW: int

No framing; raw bytes.

TcpFraming.LENGTH32 ​

ts
static LENGTH32: int

32-bit big-endian length prefix, then payload bytes.

TcpFraming.LINE ​

ts
static LINE: int

Line-delimited; packets end with \n.

TcpListenOptions class ​

ts
class TcpListenOptions

Type only: the runtime hands you instances; you never construct it yourself.

Configuration options for Tcp.listen().

TcpListenOptions.host ​

ts
host?: string

Local address to bind to (default "127.0.0.1").

TcpListenOptions.framing ​

ts
framing?: int

Packet framing mode: TcpFraming.RAW, LENGTH32, or LINE (default RAW).

TcpListenOptions.maxConnections ​

ts
maxConnections?: int

Maximum concurrent connections to accept (default unlimited).

TcpListenOptions.idleTimeout ​

ts
idleTimeout?: int

Close idle connections after this many milliseconds of inactivity (default no timeout).

TcpCloseOptions class ​

ts
class TcpCloseOptions

Type only: the runtime hands you instances; you never construct it yourself.

Configuration options for TcpServer.close().

TcpCloseOptions.closeActive ​

ts
closeActive?: boolean

If true, forcibly close all active connections; if false, gracefully drain them (default false).

TcpCloseOptions.timeoutMs ​

ts
timeoutMs?: int

Timeout in milliseconds to wait for graceful closure before forcing (default 30000).

Net static class ​

ts
static class Net

Compatibility alias for Tcp. Prefer Tcp in new code.

Net.connect ​

ts
static connect(host: string, port: int): any

Connect to a TCP server. Equivalent to Tcp.connect.

Net.listen ​

ts
static listen(port: int, hostOrOptions?: string | TcpListenOptions): any

Listen for inbound TCP connections. Equivalent to Tcp.listen.

Tcp static class ​

ts
static class Tcp

TCP socket client and server API.

Tcp.connect ​

ts
static connect(host: string, port: int): any

Connect to a TCP server at the given host and port. Returns a promise that resolves to a TcpSocket, or rejects on connection failure.

Tcp.listen ​

ts
static listen(port: int, options?: TcpListenOptions): any

Listen for inbound TCP connections on the given port. Returns a promise that resolves to a TcpServer, or rejects if the port cannot be bound.


Generated from lang/types/net.d.tks in the ThicketScript repository.