4#ifndef MALENA_WEBSOCKETCLIENT_H
5#define MALENA_WEBSOCKETCLIENT_H
15#include <nlohmann/json.hpp>
65 using BinaryCallback = std::function<void(
const std::vector<uint8_t>& data)>;
67 using CloseCallback = std::function<void(
int code,
const std::string& reason)>;
105 void send(
const std::string& message);
182 std::unique_ptr<Impl> _impl;
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.