Loading...
Searching...
No Matches
WebSocketProvider.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_WEBSOCKET_PROVIDER_H
5#define MALENA_WEBSOCKET_PROVIDER_H
6
7#pragma once
8
11#include <functional>
12#include <string>
13
14namespace ml
15{
52 {
53 public:
54 explicit WebSocketProvider(const std::string& keyField = "type");
55
56 // ── Connection ───────────────────────────────────────────────────────
57
67 void connect(const std::string& url);
68
70 void disconnect();
71
73 void send(const json& msg);
74
76 void send(const std::string& text);
77
78 // ── Lifecycle callbacks ──────────────────────────────────────────────
79
81 void onOpen(std::function<void()> callback);
82
84 void onClose(std::function<void(int, const std::string&)> callback);
85
87 void onError(std::function<void(const std::string&)> callback);
88
89 // ── Reconnection ─────────────────────────────────────────────────────
90
92 void setReconnectDelay(int seconds);
93
96
97 private:
99 };
100
101} // namespace ml
102
103#endif // MALENA_WEBSOCKET_PROVIDER_H
NetworkDispatcher(const std::string &keyField="type")
Persistent WebSocket client with main-thread callback delivery.
void onOpen(std::function< void()> callback)
Called on the main thread when the connection opens.
void send(const json &msg)
Send a JSON object as a UTF-8 text frame.
WebSocketProvider(const std::string &keyField="type")
void disconnect()
Close the connection gracefully.
void onError(std::function< void(const std::string &)> callback)
Called on the main thread on a protocol or network error.
void connect(const std::string &url)
Open a WebSocket connection to url.
void send(const std::string &text)
Send a raw text frame.
void setMaxReconnectAttempts(int n)
Maximum reconnect attempts. Pass -1 for unlimited.
void setReconnectDelay(int seconds)
Delay in seconds before reconnecting after an unexpected close.
void onClose(std::function< void(int, const std::string &)> callback)
Called on the main thread when the connection closes.
nlohmann::json json
Alias for nlohmann::json.
Definition Json.h:32
#define MALENA_API
Definition Component.h:22