12 #include "libwebsockets.h"
13 #include "libwebsockets/lws-service.h"
54 virtual int getId()
const {
return id; }
102 lws_context* context =
nullptr;
103 std::vector<Session*> sessions;
104 std::map<int, Session*> sessionMap;
112 template <
typename T>
120 WebServer(
int port = 8081,
const std::string& webDir =
".")
134 template <
typename T,
typename STATE>
144 const std::string& webDir =
".")
158 #include "picojson.h"
183 static std::string buf =
"";
185 std::string err = picojson::parse(val, msg);
186 if (err.empty() && val.is<picojson::object>()) {
191 err = picojson::parse(val, buf);
192 if (err.empty() && val.is<picojson::object>()) {
200 #include "util/json.h"
215 std::string cmd = data[
"command"];
218 returnValue[
"id"] = data[
"id"];
Abstract class for a session supporting JSON communication.
Definition: WebServer.h:164
void receiveMessage(const std::string &msg)
Receives a message from the client and parses it as JSON.
Definition: WebServer.h:182
virtual void receiveJSON(picojson::value &val)
Receives a JSON message from the client.
Definition: WebServer.h:170
virtual void sendJSON(picojson::value &val)
Sends a JSON message to the client.
Definition: WebServer.h:176
Manages a picojson::object, works with JsonValue to provide implicit casting.
Abstract class for a session supporting JSON communication with command handling, inherits from JSONS...
Definition: WebServer.h:206
void receiveJSON(picojson::value &val)
Receive a command from the web server.
Definition: WebServer.h:212
virtual void receiveCommand(const std::string &cmd, const JsonObject &data, JsonObject &returnValue)=0
Handles specific commands from the web server.
Manages a picojson::value, provides implicit casting for valid JSON types.
Definition: json.h:20
picojson::value & getValue()
Definition: json.h:118
Represents a session with a client.
Definition: WebServer.h:37
virtual int getId() const
Returns the ID of the session.
Definition: WebServer.h:54
Session()
Constructor for Session.
virtual ~Session()
Destructor.
virtual void sendMessage(const std::string &msg)
Sends a message to the client.
virtual void receiveMessage(const std::string &msg)
Receives a message from the client.
Definition: WebServer.h:60
virtual void onWrite()
Function called when the session is ready to write.
virtual void update()
Performs any necessary updates for the session.
Definition: WebServer.h:71
Base class for web server implementation.
Definition: WebServer.h:19
virtual ~WebServerBase()
Destructor.
WebServerBase(int port=8081, const std::string &webDir=".")
Constructor for WebServerBase.
virtual void createSession(void *info)
Creates a session for a new client connection.
void service(int time=10)
Services the server, processing incoming requests.
virtual Session * createSession()
Factory method to create a new session.
Definition: WebServer.h:99
Templated class for a web server with state that inherits from WebServerBase.
Definition: WebServer.h:135
WebServerWithState(STATE state, int port=8081, const std::string &webDir=".")
Constructor for WebServerWithState that calls WebServerBase's constructor.
Definition: WebServer.h:143
Session * createSession()
Factory method to create a new session of type T with the given state.
Definition: WebServer.h:151
Templated class for a basic web server that inherits from WebServerBase.
Definition: WebServer.h:113
WebServer(int port=8081, const std::string &webDir=".")
Constructor for WebServer that calls WebServerBase's constructor.
Definition: WebServer.h:120
Session * createSession()
Factory method to create a new session of type T.
Definition: WebServer.h:127