net
TCP sockets.
Import
ts
import { TcpSocket, TcpServer, TcpFraming, Net, ... } from "net";Needs the "net" permission in thicket.json.
TcpSocket class
ts
class TcpSocketA connected TCP socket for bidirectional communication.
TcpSocket.remoteAddress
ts
remoteAddress: stringRemote peer address (IP or hostname).
TcpSocket.remotePort
ts
remotePort: intRemote peer port.
TcpSocket.localPort
ts
localPort: intLocal bound port.
TcpSocket.write
ts
write(data: string | ByteArray): booleanWrite 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(): voidClose the socket, terminating the connection.
TcpSocket.onData
ts
onData(fn: (data: string) => void): voidRegister 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): voidRegister 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): voidRegister a callback invoked when the socket closes. @param fn Callback with no arguments.
TcpSocket.onError
ts
onError(fn: (error: string) => void): voidRegister a callback invoked on socket errors. @param fn Callback receiving an error message string.
TcpSocket.setNoDelay
ts
setNoDelay(enabled: boolean): voidEnable or disable Nagle's algorithm. Enabled by default; set to false for real-time data.
TcpSocket.setKeepAlive
ts
setKeepAlive(enabled: boolean, delaySeconds?: int): voidEnable 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): voidRegister 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): voidRegister a callback invoked when the write buffer drains (after backpressure clears). @param fn Callback with no arguments.
TcpSocket.writePacket
ts
writePacket(data: ByteArray): booleanWrite a framed packet to the socket if framing is enabled. Returns true if accepted, false if backpressured.
TcpServer class
ts
class TcpServerA listening TCP server socket that accepts inbound client connections.
TcpServer.address
ts
address: stringLocal address the server is bound to.
TcpServer.port
ts
port: intLocal port the server is listening on.
TcpServer.onConnection
ts
onConnection(fn: (socket: TcpSocket) => void): voidRegister 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: intCurrent number of active client connections.
TcpServer.connections
ts
connections: Array<TcpSocket>Array of active client TcpSocket connections.
TcpServer.broadcast
ts
broadcast(data: string | ByteArray): booleanBroadcast data (string or ByteArray) to all connected clients. Returns true if all accepts succeeded, false if any failed.
TcpFraming class
ts
class TcpFramingPacket framing modes for TCP sockets.
TcpFraming.RAW
ts
static RAW: intNo framing; raw bytes.
TcpFraming.LENGTH32
ts
static LENGTH32: int32-bit big-endian length prefix, then payload bytes.
TcpFraming.LINE
ts
static LINE: intLine-delimited; packets end with \n.
TcpListenOptions class
ts
class TcpListenOptionsType only: the runtime hands you instances; you never construct it yourself.
Configuration options for Tcp.listen().
TcpListenOptions.host
ts
host?: stringLocal address to bind to (default "127.0.0.1").
TcpListenOptions.framing
ts
framing?: intPacket framing mode: TcpFraming.RAW, LENGTH32, or LINE (default RAW).
TcpListenOptions.maxConnections
ts
maxConnections?: intMaximum concurrent connections to accept (default unlimited).
TcpListenOptions.idleTimeout
ts
idleTimeout?: intClose idle connections after this many milliseconds of inactivity (default no timeout).
TcpCloseOptions class
ts
class TcpCloseOptionsType only: the runtime hands you instances; you never construct it yourself.
Configuration options for TcpServer.close().
TcpCloseOptions.closeActive
ts
closeActive?: booleanIf true, forcibly close all active connections; if false, gracefully drain them (default false).
TcpCloseOptions.timeoutMs
ts
timeoutMs?: intTimeout in milliseconds to wait for graceful closure before forcing (default 30000).
Net static class
ts
static class NetCompatibility alias for Tcp. Prefer Tcp in new code.
Net.connect
ts
static connect(host: string, port: int): anyConnect to a TCP server. Equivalent to Tcp.connect.
Net.listen
ts
static listen(port: int, hostOrOptions?: string | TcpListenOptions): anyListen for inbound TCP connections. Equivalent to Tcp.listen.
Tcp static class
ts
static class TcpTCP socket client and server API.
Tcp.connect
ts
static connect(host: string, port: int): anyConnect 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): anyListen 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.