Persistent WebSocket client with main-thread callback delivery.
More...
#include <Malena/Engine/Networking/WebSocketClient.h>
|
| using | BinaryCallback = std::function<void(const std::vector<uint8_t>& data)> |
| using | CloseCallback = std::function<void(int code, const std::string& reason)> |
| using | ErrorCallback = std::function<void(const std::string& error)> |
| using | MessageCallback = std::function<void(const std::string& message)> |
| using | OpenCallback = std::function<void()> |
Persistent WebSocket client with main-thread callback delivery.
WebSocketClient maintains a single WebSocket connection on a background thread. All callbacks — onOpen, onMessage, onClose, onError — are queued and delivered on the main thread at the next frame flush, so you can safely update UI state inside them.
Basic usage
ws.
send(R
"({"type":"subscribe","channel":"questions"})");
});
ws.onMessage([this](
const std::string& msg) {
handleServerEvent(msg);
});
ws.
onClose([](
int code,
const std::string& reason) {
std::cout << "Closed " << code << ": " << reason << "\n";
});
ws.
onError([](
const std::string& err) {
std::cerr <<
"WS error: " <<
err <<
"\n";
});
ws.
connect(
"wss://api.example.com/events");
Persistent WebSocket client with main-thread callback delivery.
void onMessage(MessageCallback cb)
Fired on the main thread when a UTF-8 text frame arrives.
void connect(const std::string &url)
Connect to url on a background thread.
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 onClose(CloseCallback cb)
Fired on the main thread when the connection closes.
void onError(ErrorCallback cb)
Fired on the main thread when a protocol or network error occurs.
SFML_SYSTEM_API std::ostream & err()
Reconnection
By default the client does not reconnect on unexpected closure. Enable automatic reconnection with a back-off delay:
void setMaxReconnectAttempts(int n)
Maximum number of reconnection attempts before giving up.
void setReconnectDelay(int seconds)
Set automatic reconnect delay in seconds.
- See also
- HttpClient, NetworkManager
Definition at line 61 of file WebSocketClient.h.
◆ BinaryCallback
◆ CloseCallback
◆ ErrorCallback
◆ MessageCallback
◆ OpenCallback
◆ WebSocketClient() [1/2]
| ml::WebSocketClient::WebSocketClient |
( |
| ) |
|
◆ ~WebSocketClient()
| ml::WebSocketClient::~WebSocketClient |
( |
| ) |
|
◆ WebSocketClient() [2/2]
| ml::WebSocketClient::WebSocketClient |
( |
const WebSocketClient & | | ) |
|
|
delete |
◆ connect()
| void ml::WebSocketClient::connect |
( |
const std::string & | url | ) |
|
Connect to url on a background thread.
Supports ws:// and wss:// (TLS) schemes. Fires onOpen when the handshake completes.
- Parameters
-
| url | WebSocket URL, e.g. "wss://api.example.com/events". |
◆ disconnect()
| void ml::WebSocketClient::disconnect |
( |
| ) |
|
Close the connection gracefully.
Fires onClose with code 1000 (normal closure). Safe to call when already disconnected.
◆ isConnected()
| bool ml::WebSocketClient::isConnected |
( |
| ) |
const |
|
nodiscard |
Return true while the connection is open.
◆ onBinary()
Fired on the main thread when a binary frame arrives.
- Parameters
-
| cb | Callback receiving the binary payload. |
◆ onClose()
Fired on the main thread when the connection closes.
- Parameters
-
| cb | Callback receiving the WebSocket close code and reason. |
◆ onError()
Fired on the main thread when a protocol or network error occurs.
- Parameters
-
| cb | Callback receiving an error description. |
◆ onMessage()
Fired on the main thread when a UTF-8 text frame arrives.
- Parameters
-
◆ onOpen()
Fired on the main thread when the connection opens.
Use this to send an initial subscription message.
◆ operator=()
◆ send() [1/2]
| void ml::WebSocketClient::send |
( |
const nlohmann::json & | json | ) |
|
|
inline |
Serialise json and send it as a UTF-8 text frame.
Equivalent to calling send(json.dump()).
ws.send({{"type", "subscribe"}, {"channel", "questions"}});
ws.send({{"type", "answer"}, {"questionId", 7}, {"value", "RAII"}});
Definition at line 123 of file WebSocketClient.h.
◆ send() [2/2]
| void ml::WebSocketClient::send |
( |
const std::string & | message | ) |
|
Send a UTF-8 text frame.
- Parameters
-
◆ sendBinary()
| void ml::WebSocketClient::sendBinary |
( |
const std::vector< uint8_t > & | data | ) |
|
Send a binary frame.
- Parameters
-
◆ setMaxReconnectAttempts()
| void ml::WebSocketClient::setMaxReconnectAttempts |
( |
int | n | ) |
|
Maximum number of reconnection attempts before giving up.
- Parameters
-
| n | Pass -1 for unlimited attempts. |
◆ setReconnectDelay()
| void ml::WebSocketClient::setReconnectDelay |
( |
int | seconds | ) |
|
Set automatic reconnect delay in seconds.
When non-zero, the client reconnects after an unexpected closure. The delay doubles on each failed attempt up to maxReconnectAttempts.
- Parameters
-
| seconds | Delay before the first reconnect attempt. 0 = disabled. |
The documentation for this class was generated from the following file: