Net API

The Net API provides AngelScript access to:

  • HTTP(S) requests

  • WebSocket (ws / wss) connections

  • Background message buffering

  • Safe multi-threaded operation

  • Automatic cleanup on script unload


Overview

HTTP

  • net_http_get(url, status, body, timeout_ms)

  • net_http_post(url, content_type, body, status, out_body, timeout_ms)

WebSocket

  • ws_t ws_connect(url, timeout_ms)

  • ws_t methods:

    • is_open()

    • send_text()

    • send_json()

    • send_binary()

    • recv() (blocking)

    • poll() (non-blocking)

    • close()

Supported URL Schemes

Type
Schemes

HTTP

http://, https://

WebSocket

ws://, wss://

Hostnames

example.com, sub.domain.net

IPs

127.0.0.1, 192.168.x.x

Ports

Fully supported: ws://ip:port/path

ws:// and wss:// automatically map to WinHTTP’s http:// / https:// internally.


HTTP API

bool net_http_get(...)

Parameters

Name
Type
Description

url

string

Full URL (https://httpbin.org/get)

status_code

out uint

HTTP status (200, 404, etc.)

body

out string

Response body

timeout_ms

uint

Optional timeout (resolve/connect/send/receive)

Returns

true = request succeeded (transport level, not HTTP success) false = network/connection error

Example


bool net_http_post(...)

Example


WebSocket API

WebSockets are exposed as a handle type:

Internally, each websocket:

  • Opens a WinHTTP WebSocket handle

  • Launches a background message receive thread

  • Buffers complete messages into a protected queue

  • Cleans up automatically on script unload (using AS ref-tracking like proc_t)


ws_t ws_connect(...)

Return value

  • A valid ws_t handle on success

  • 0 on failure

Example


WebSocket Methods

All websocket operations are methods on the ws_t type.


bool ws_t::is_open() const

Returns true while:

  • The socket is open, AND

  • The background receive thread is still running.


bool ws_t::send_text(const string &in msg)

Sends a UTF-8 text frame.


bool ws_t::send_json(const string &in json)

Sends a JSON UTF-8 message.

Example:


bool ws_t::send_binary(const array<uint8> &in data)

Sends a binary WebSocket frame.

Some public echo servers may close on binary frames. Your API supports them natively.


Receiving Messages

Messages are delivered as complete frames into an internal queue.


bool ws_t::recv(string &out msg, bool &out is_text)

Blocking receive (waits for next message).

  • Returns true when a message is dequeued.

  • Blocks with a 1ms sleep inside the engine.

  • Returns false if the socket closed and queue is empty.

Example


bool ws_t::poll(string &out msg, bool &out is_text, bool &out is_closed)

Non-blocking check.

Outcomes

  1. Message available

    • Returns true

    • Fills msg

    • is_text = true/false

    • is_closed reflects final socket state

  2. No message yet

    • Returns false

    • msg = ""

    • is_closed = false

  3. Socket closed and queue empty

    • Returns false

    • is_closed = true

Example (frame update)


Closing

void ws_t::close(uint16 code = 1000)

  • Signals the receive thread to stop

  • Sends a close frame

  • Waits for the thread to terminate

  • Frees all WinHTTP handles

  • Removes itself from the internal AS websocket ref-tracker

Example


Automatic Cleanup (Important)

This API integrates with your existing AngelScript resource tracking model:

  • All websockets created within a script are tracked

  • On script unload, the engine loops through these refs and frees them

This guarantees:

  • No leaked sockets

  • No zombie threads

  • Safe hot-reload


Full Example


Summary

HTTP

  • net_http_get(url, out status, out body, timeout)

  • net_http_post(url, content_type, body, out status, out response, timeout)

WebSocket

  • ws_t ws_connect(url, timeout)

  • ws_t methods:

    • is_open()

    • send_text()

    • send_json()

    • send_binary()

    • recv(out msg, out is_text)

    • poll(out msg, out is_text, out is_closed)

    • close(code)

Last updated