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_tmethods:is_open()send_text()send_json()send_binary()recv()(blocking)poll()(non-blocking)close()
Supported URL 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://andwss://automatically map to WinHTTP’shttp:///https://internally.
HTTP API
bool net_http_get(...)
bool net_http_get(...)bool net_http_get(
const string &in url,
uint &out status_code,
string &out body,
uint timeout_ms = 0
);Parameters
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
uint status;
string body;
bool ok = net_http_get("https://httpbin.org/get", status, body, 5000);
if (ok)
{
log("GET " + status);
log("Body: " + body);
}bool net_http_post(...)
bool net_http_post(...)bool net_http_post(
const string &in url,
const string &in content_type,
const string &in body,
uint &out status_code,
string &out response,
uint timeout_ms = 0
);Example
uint status;
string response;
bool ok = net_http_post(
"https://httpbin.org/post",
"application/json",
"{\"hello\":\"pcx\"}",
status,
response,
5000
);WebSocket API
WebSockets are exposed as a handle type:
ws_t ws_connect(const string &in url, uint timeout_ms = 0);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(...)
ws_t ws_connect(...)ws_t ws_connect(const string &in url, uint timeout_ms = 0);Return value
A valid
ws_thandle on success0on failure
Example
ws_t ws = ws_connect("wss://ws.postman-echo.com/raw", 5000);
if (ws == 0)
{
log("Connection failed");
return;
}
log("Connected");WebSocket Methods
All websocket operations are methods on the ws_t type.
bool ws_t::is_open() const
bool ws_t::is_open() constReturns true while:
The socket is open, AND
The background receive thread is still running.
bool ws_t::send_text(const string &in msg)
bool ws_t::send_text(const string &in msg)Sends a UTF-8 text frame.
ws.send_text("hello from AS!");bool ws_t::send_json(const string &in json)
bool ws_t::send_json(const string &in json)Sends a JSON UTF-8 message.
Example:
ws.send_json("{\"type\":\"ping\",\"from\":\"as\"}");bool ws_t::send_binary(const array<uint8> &in data)
bool ws_t::send_binary(const array<uint8> &in data)Sends a binary WebSocket frame.
array<uint8> bin = { 0, 1, 2, 3 };
ws.send_binary(bin);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)
bool ws_t::recv(string &out msg, bool &out is_text)Blocking receive (waits for next message).
Returns
truewhen a message is dequeued.Blocks with a 1ms sleep inside the engine.
Returns
falseif the socket closed and queue is empty.
Example
string msg;
bool isText;
if (ws.recv(msg, isText))
log("Received: " + msg);bool ws_t::poll(string &out msg, bool &out is_text, bool &out is_closed)
bool ws_t::poll(string &out msg, bool &out is_text, bool &out is_closed)Non-blocking check.
Outcomes
Message available
Returns
trueFills
msgis_text = true/falseis_closedreflects final socket state
No message yet
Returns
falsemsg = ""is_closed = false
Socket closed and queue empty
Returns
falseis_closed = true
Example (frame update)
string msg;
bool text, closed;
bool has = ws.poll(msg, text, closed);
if (has)
{
log("WS message: " + msg);
}
else if (closed)
{
log("WS closed");
}Closing
void ws_t::close(uint16 code = 1000)
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
ws.close();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
int main()
{
uint status;
string body;
// HTTP
if (net_http_get("https://httpbin.org/get", status, body, 3000))
log("GET OK: " + status);
// WS
ws_t ws = ws_connect("wss://ws.postman-echo.com/raw", 3000);
ws.send_text("hello from AS");
string msg; bool text;
if (ws.recv(msg, text))
log("Echo: " + msg);
ws.close();
return 0;
}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_tmethods: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