ws - WebSocket client

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

#include "kse_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);

Examples

// test_ws.ks - WebSocket plugin tests
// Uses a public echo server: anything sent is echoed back.
//
// After installing:
//   minks install kse_ws   (run once from inside ws_plugin/)
//   minks test_ws.ks
//
// Without installing:
//   minks --plugin-path . test_ws.ks

#include "kse_ws"

Konsol:Print("=== WebSocket Plugin Test ===");
Konsol:Print("");

Var:Number conn;
try {
    WS:Connect("wss://echo.websocket.events", conn);
} catch (WSException e) {
    Konsol:Print("Connect failed: ${e.message}");
    Konsol:Exit(1);
}
Konsol:Print("Connected, handle: ${conn}");
Konsol:Print("");

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

// -- Send and receive echo ----------------------------------------------------

Konsol:Print("--- Send / Recv");
WS:Send(conn, "hello from KonsolScript", ok);
Konsol:Print("Send: ${ok}");

// poll until echo arrives (non-blocking Recv returns "" when no frame ready)
Var:Number attempts = 0;
msg = "";
while (msg == "" && attempts < 50) {
    WS:Recv(conn, msg);
    Konsol:Delay(50);
    attempts = attempts + 1;
}
Konsol:Print("Recv: ${msg}");
Konsol:Print("");

// -- Multiple messages --------------------------------------------------------

Konsol:Print("--- Multiple messages");
WS:Send(conn, "message 1", ok);
WS:Send(conn, "message 2", ok);
WS:Send(conn, "message 3", ok);

Var:Number received = 0;
attempts = 0;
while (received < 3 && attempts < 100) {
    WS:Recv(conn, msg);
    if (msg != "") {
        Konsol:Print("Echo: ${msg}");
        received = received + 1;
    }
    Konsol:Delay(30);
    attempts = attempts + 1;
}
Konsol:Print("");

// -- IsConnected / Error ------------------------------------------------------

Konsol:Print("--- Status");
WS:IsConnected(conn, alive);
Konsol:Print("IsConnected: ${alive}");

Var:String errMsg;
WS:Error(conn, errMsg);
Konsol:Print("Error: '${errMsg}'");
Konsol:Print("");

// -- Connect with custom headers ----------------------------------------------

Konsol:Print("--- ConnectHeaders");
Var:Number conn2;
try {
    WS:ConnectHeaders("wss://echo.websocket.events", "X-Client: minks\nUser-Agent: KonsolScript", conn2);
    WS:Send(conn2, "hello with headers", ok);
    msg = "";
    attempts = 0;
    while (msg == "" && attempts < 50) {
        WS:Recv(conn2, msg);
        Konsol:Delay(50);
        attempts = attempts + 1;
    }
    Konsol:Print("Echo: ${msg}");
    WS:Close(conn2);
} catch (WSException e) {
    Konsol:Print("ConnectHeaders failed: ${e.message}");
}
Konsol:Print("");

// -- Done ---------------------------------------------------------------------

WS:Close(conn);
Konsol:Print("=== done ===");


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