4.4. High-level HTTP and WebSocket wrappers
The DASHV_BOOST module provides high-level daScript wrapper classes for the
low-level dashv C++ bindings.
HvWebServer wraps the WebSocket/HTTP server with convenient route
registration (GET, POST, PUT, DELETE, PATCH, HEAD,
ANY), static file serving, and WebSocket event callbacks.
HvWebSocketClient wraps the WebSocket client with onOpen,
onClose, and onMessage callbacks.
All functions and symbols are in “dashv_boost” module, use require to get access to it.
require dashv/dashv_boost
See also:
4.4.1. Classes
- HvWebSocketClient
WebSocket client with event-driven callbacks.
Subclass and override onOpen, onClose, and onMessage
to handle connection lifecycle and incoming messages. Call
process_event_que periodically to dispatch events.
Called when the WebSocket connection is established.
Called when the WebSocket connection is closed.
- Fields:
client : WebSocketClient? - Called when a text message is received from the server.
- HvWebSocketClient.init(url: string): int
Connects to the given WebSocket URL. Returns 0 on success.
- Arguments:
url : string
- HvWebSocketClient.is_connected(): bool
Returns true if the WebSocket connection is currently open.
- HvWebSocketClient.process_event_que()
Processes pending WebSocket events; must be called periodically.
- HvWebSocketClient.send(text: string)
Sends a text message to the server.
- Arguments:
text : string
- HvWebSocketClient.close(): int
Closes the WebSocket connection. Returns 0 on success.
- HvWebServer
HTTP and WebSocket server with route registration and event callbacks.
Subclass and override onWsOpen, onWsClose, onWsMessage,
onTick, and onInit as needed. Register HTTP routes with
GET, POST, PUT, DELETE, PATCH, HEAD, or ANY.
Called when a WebSocket client connects. Override to handle new connections.
Called when a WebSocket client disconnects. Override to handle disconnections.
Called when a WebSocket message is received. Override to handle messages.
Called each tick cycle. Override for periodic work.
- Fields:
server : WebSocketServer? - Called during server initialization, after the low-level server is created. Override to register routes.
- HvWebServer.init(port: int)
Initializes an HTTP/WebSocket server on the specified port.
- Arguments:
port : int
- HvWebServer.init_wss(port: int; httpsPort: int; pathToCert: string = "")
Initializes an HTTPS/WSS server with TLS. Uses the default modules/dasHV/cert
directory if pathToCert is empty.
- Arguments:
port : int
httpsPort : int
pathToCert : string
- HvWebServer.start(): int
Starts the server. Returns 0 on success.
- HvWebServer.stop(): int
Stops the server. Returns 0 on success.
- HvWebServer.tick()
Processes pending HTTP and WebSocket events; must be called periodically.
- HvWebServer.onWsOpen(channel: WebSocketChannel?; url: string#)
def onWsOpen : function<(var self:dashv_boost::HvWebServer;channel:dashv::WebSocketChannel? const;url:string const#):void>
- Arguments:
channel : WebSocketChannel?
url : string#
- HvWebServer.onWsClose(channel: WebSocketChannel?)
def onWsClose : function<(var self:dashv_boost::HvWebServer;channel:dashv::WebSocketChannel? const):void>
- Arguments:
channel : WebSocketChannel?
- HvWebServer.onWsMessage(channel: WebSocketChannel?; msg: string#)
def onWsMessage : function<(var self:dashv_boost::HvWebServer;channel:dashv::WebSocketChannel? const;msg:string const#):void>
- Arguments:
channel : WebSocketChannel?
msg : string#
- HvWebServer.onTick()
def onTick : function<(var self:dashv_boost::HvWebServer):void>
- HvWebServer.onInit()
def onInit : function<(var self:dashv_boost::HvWebServer):void>
- HvWebServer.GET(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP GET requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.POST(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP POST requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.PUT(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP PUT requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.DELETE(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP DELETE requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.PATCH(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP PATCH requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.HEAD(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for HTTP HEAD requests matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.ANY(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers a handler for any HTTP method matching uri.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
- HvWebServer.STATIC(path: string; dir: string)
Serves static files from dir under the URL prefix path.
- Arguments:
path : string
dir : string
- HvWebServer.allow_cors()
Enables cross-origin resource sharing (CORS) on the server.
- HvWebServer.set_document_root(dir: string)
Sets the document root directory for static file serving.
- Arguments:
dir : string
- HvWebServer.set_home_page(filename: string)
Sets the default home page file (e.g., index.html).
- Arguments:
filename : string
- HvWebServer.set_index_of(dir: string)
Enables directory listing for the specified directory.
- Arguments:
dir : string
- HvWebServer.set_error_page(filename: string)
Sets a custom error page file.
- Arguments:
filename : string
- HvWebServer.SSE(uri: string; lmb: lambda<(var req:HttpRequest?;var resp:HttpResponse?):http_status>)
Registers an SSE (Server-Sent Events) handler for uri.
Same signature as GET/POST — set SSE headers and write events to the response.
Uses ANY method matching so any HTTP method reaches this handler.
- Arguments:
uri : string
lmb : lambda<(req: HttpRequest?;resp: HttpResponse?): http_status>
4.4.2. HTTP request helpers
- get_body_bytes(resp: HttpResponse?): array<uint8>
Extracts the response body as an array<uint8>.
Returns an empty array if the response is null, non-OK, or has no content.
- Arguments:
resp : HttpResponse?
- with_http_request(blk: block<(var req:HttpRequest?#):void>)
Creates a temporary HttpRequest, invokes the block, then cleans up.
Use this to configure and send outbound HTTP requests.
- Arguments:
blk : block<(req: HttpRequest?#):void>