Loading...
Searching...
No Matches
WebSocketClient.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_WEBSOCKETCLIENT_H
5#define MALENA_WEBSOCKETCLIENT_H
6
7#pragma once
8
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <string>
14#include <vector>
15#include <nlohmann/json.hpp>
16
17namespace ml
18{
62 {
63 public:
64 using MessageCallback = std::function<void(const std::string& message)>;
65 using BinaryCallback = std::function<void(const std::vector<uint8_t>& data)>;
66 using OpenCallback = std::function<void()>;
67 using CloseCallback = std::function<void(int code, const std::string& reason)>;
68 using ErrorCallback = std::function<void(const std::string& error)>;
69
72
75
76 // ── Connection ───────────────────────────────────────────────────────
77
86 void connect(const std::string& url);
87
94 void disconnect();
95
97 [[nodiscard]] bool isConnected() const;
98
99 // ── Sending ──────────────────────────────────────────────────────────
100
105 void send(const std::string& message);
106
111 void sendBinary(const std::vector<uint8_t>& data);
112
123 void send(const nlohmann::json& json)
124 {
125 send(json.dump());
126 }
127
128 // ── Callbacks ────────────────────────────────────────────────────────
129
136
142
148
154
160
161 // ── Reconnection ─────────────────────────────────────────────────────
162
171 void setReconnectDelay(int seconds);
172
178
179 private:
181 struct Impl;
182 std::unique_ptr<Impl> _impl;
184 };
185
186} // namespace ml
187
188#endif // MALENA_WEBSOCKETCLIENT_H
bool isConnected() const
Return true while the connection is open.
std::function< void(const std::vector< uint8_t > &data)> BinaryCallback
void setMaxReconnectAttempts(int n)
Maximum number of reconnection attempts before giving up.
std::function< void()> OpenCallback
void send(const nlohmann::json &json)
Serialise json and send it as a UTF-8 text frame.
void onMessage(MessageCallback cb)
Fired on the main thread when a UTF-8 text frame arrives.
std::function< void(int code, const std::string &reason)> CloseCallback
void setReconnectDelay(int seconds)
Set automatic reconnect delay in seconds.
void connect(const std::string &url)
Connect to url on a background thread.
void sendBinary(const std::vector< uint8_t > &data)
Send a binary frame.
WebSocketClient & operator=(const WebSocketClient &)=delete
std::function< void(const std::string &error)> ErrorCallback
WebSocketClient(const WebSocketClient &)=delete
void onOpen(OpenCallback cb)
Fired on the main thread when the connection opens.
void send(const std::string &message)
Send a UTF-8 text frame.
void disconnect()
Close the connection gracefully.
void onClose(CloseCallback cb)
Fired on the main thread when the connection closes.
std::function< void(const std::string &message)> MessageCallback
void onBinary(BinaryCallback cb)
Fired on the main thread when a binary frame arrives.
void onError(ErrorCallback cb)
Fired on the main thread when a protocol or network error occurs.
nlohmann::json json
Alias for nlohmann::json.
Definition Json.h:32
#define MALENA_API
Definition Component.h:22