6#ifndef PLOTLY_DETAILS_JSON_RPC_HPP
7#define PLOTLY_DETAILS_JSON_RPC_HPP
10#include <nlohmann/json.hpp>
19#include <unordered_set>
49 std::optional<nlohmann::json>
id;
50 std::optional<nlohmann::json>
52 std::optional<JsonRpcError>
error;
59 [[nodiscard]]
auto toJson() const -> nlohmann::json;
70 explicit JsonRpc(std::unique_ptr<WebsocketEndpointInterface> wsEndpoint);
92 const std::string &method,
93 const std::function<nlohmann::json(
const nlohmann::json &)> &handler);
97 std::function<
void(
const nlohmann::json &)> handler);
108 const std::string &callbackName,
109 std::function<
void(
const std::string_view)> callback);
129 auto call(
const std::string &method,
const nlohmann::json ¶ms)
130 -> std::pair<std::future<nlohmann::json>, std::function<void()>>;
137 void notify(
const std::string &method,
const nlohmann::json ¶ms);
149 void handleIncomingMessage(const std::string_view &message);
150 void sendSuccessResponse(const nlohmann::json &requestId,
151 const nlohmann::json &
result);
152 void sendErrorResponse(const nlohmann::json &requestId,
154 const std::
string &message);
156 std::unordered_map<std::
string,
157 std::function<nlohmann::json(const nlohmann::json &)>>
159 std::unordered_map<std::
string, std::function<
void(const nlohmann::json &)>>
161 std::unordered_set<std::
string> _registeredCallbacks;
162 mutable std::mutex _registeredCallbacksMutex;
void registerCallbackWithWebsocket(const std::string &callbackName, std::function< void(const std::string_view)> callback)
Registers a callback function for the WebSocket endpoint.
Definition json_rpc.cpp:100
auto operator=(const JsonRpc &) -> JsonRpc &=delete
void unregisterAllCallbacksFromWebsockets()
Unregisters all callbacks from the WebSocket endpoint.
Definition json_rpc.cpp:122
auto call(const std::string &method, const nlohmann::json ¶ms) -> std::pair< std::future< nlohmann::json >, std::function< void()> >
Makes an asynchronous JSON-RPC call to the connected client.
Definition json_rpc.cpp:130
void unregisterCallbackFromWebsocket(const std::string &callbackName)
Unregisters a callback function from the WebSocket endpoint.
Definition json_rpc.cpp:112
void registerHandler(const std::string &method, const std::function< nlohmann::json(const nlohmann::json &)> &handler)
Registers a handler function for a specific RPC method.
Definition json_rpc.cpp:82
void registerNotification(const std::string &method, std::function< void(const nlohmann::json &)> handler)
Definition json_rpc.cpp:88
JsonRpc(const JsonRpc &)=delete
void unregisterHandler(const std::string &method)
Definition json_rpc.cpp:96
void notify(const std::string &method, const nlohmann::json ¶ms)
Sends a notification to all connected clients.
Definition json_rpc.cpp:171
auto getWebsocketEndpoint() const -> WebsocketEndpointInterface *
Gets a raw pointer to the underlying websocket endpoint.
Definition json_rpc.cpp:179
JsonRpc(std::unique_ptr< WebsocketEndpointInterface > wsEndpoint)
Constructs a new JSON-RPC server.
Definition json_rpc.cpp:39
Abstract interface for WebSocket endpoints.
Definition websockets_endpoint.hpp:37
Definition browser.cpp:27
JsonRpcErrorCode
Enumeration of standard JSON-RPC 2.0 error codes.
Definition json_rpc.hpp:27
@ INVALID_PARAMS
Invalid method parameter(s)
Definition json_rpc.hpp:31
@ SERVER_ERROR
Server error.
Definition json_rpc.hpp:33
@ PARSE_ERROR
Invalid JSON was received by the server.
Definition json_rpc.hpp:28
@ METHOD_NOT_FOUND
The method does not exist / is not available.
Definition json_rpc.hpp:30
@ INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition json_rpc.hpp:29
@ INTERNAL_ERROR
Internal JSON-RPC error.
Definition json_rpc.hpp:32
Structure representing a JSON-RPC error object.
Definition json_rpc.hpp:39
nlohmann::json data
Additional error data.
Definition json_rpc.hpp:42
int code
The error code.
Definition json_rpc.hpp:40
std::string message
The error message.
Definition json_rpc.hpp:41
Structure representing a JSON-RPC response object.
Definition json_rpc.hpp:48
auto toJson() const -> nlohmann::json
Converts the response to a JSON object.
Definition json_rpc.cpp:19
std::string jsonrpc
JSON-RPC version string.
Definition json_rpc.hpp:53
std::optional< nlohmann::json > id
Request ID (null for notifications)
Definition json_rpc.hpp:49
std::optional< nlohmann::json > result
The result of the request (if successful)
Definition json_rpc.hpp:51
std::optional< JsonRpcError > error
The error object (if request failed)
Definition json_rpc.hpp:52
WebSocket endpoint interface and implementation for plotly.cpp.