ws — WebSocket client

Requires: libcurl ≥ 7.86 (auto-installed via minks install ws)

#include "ws"

Non-blocking WebSocket client. WS:Recv returns "" immediately if no frame is ready — call it in a poll loop.


Method Args Out Description
WS:Connect url Number Open WebSocket connection (ws:// or wss://); returns handle. Throws WSException on failure
WS:ConnectHeaders url, headers Number Connect with custom headers; headers = newline-separated "Key: Value" pairs
WS:Send handle, message Boolean Send a text frame
WS:SendBinary handle, data Boolean Send a binary frame
WS:Recv handle String Receive next frame; "" if none ready (non-blocking)
WS:Close handle void Send close frame and release handle
WS:IsConnected handle Boolean false after a close frame is received
WS:Error handle String Last error message, "" if none

Throws WSException on connection failure. See Object — Exception.

Var:Number conn;
Var:Boolean ok;
Var:String msg;
Var:Boolean alive;

try {
    WS:Connect("ws://localhost:8080/chat", conn);
} catch (WSException e) {
    Konsol:Print("connect failed: ${e.message}");
}

WS:Send(conn, "hello", ok);

while (true) {
    WS:Recv(conn, msg);
    if (msg != "") { Konsol:Print("received: ${msg}"); }
    WS:IsConnected(conn, alive);
    if (!alive) { break; }
}

WS:Close(conn);

Building from source

Same dependency as curl_plugin. Requires libcurl ≥ 7.86 for WebSocket support.

Platform Command
Debian/Ubuntu sudo apt install libcurl4-openssl-dev
Fedora/RHEL sudo dnf install libcurl-devel
macOS brew install curl
MSYS2 MINGW64 pacman -S mingw-w64-x86_64-curl
MSYS2 UCRT64 pacman -S mingw-w64-ucrt-x86_64-curl
MSYS2 CLANG64 pacman -S mingw-w64-clang-x86_64-curl
MSYS2 CLANGARM64 pacman -S mingw-w64-clang-aarch64-curl

For native Windows, download a prebuilt libcurl ≥ 7.86 from curl.se/windows.

make -C ws_plugin