For my Bachelor thesis I need to enable communication (JSON) between a GUI coded in HTML/JS and a program in C++. For this I tried to use civetweb to create a server and send some dummy data from C++ to the GUI. The idea is that the user sets some parameters on the GUI, and then the C++ part computes everything and sends back the result. As I don't know so much about internet connection, I have trouble connecting both ends of my project.
On the C++ part, I could create a server which shows the correct webpage. I just don't know how exactly to connect it afterwards. I guess I would need one of the handle to send data (post or put) and another one to receive data (get), but not sure how to use those.
std::vector<std::string> options
= {
"listening_ports", "8080",
"document_root", "..\\res\\html",
"enable_directory_listing", "yes",
"static_file_max_age", "0"
};
CivetServer *server = new CivetServer(options);
CivetHandler *handler = new CivetHandler();
server->addHandler("/json", handler);
ShellExecuteA(NULL, NULL, "http://localhost:8080/index.html", NULL, NULL, SW_SHOW); // open browser
std::stringstream responseStream;
responseStream << "Content-Type: application/json\r\n"
<< "{"
<< "\"result\": "
<< "\"dummy\""
<< "}";
char buffer[1000] = "";
mg_connection *conn;
conn = mg_connect_client("localhost", 8080, 0, buffer, 1000);
handler->handlePost(server, conn);
std::stringstream str;
str << "HTTP/1.0 200 OK\r\n"
<< responseStream.str()
<< "\r\n";
mg_printf(conn, "%s", str.str().c_str());
mg_printf gives back some positive value the first seconds, then switches to -2. Is this the timeout of the server ?
And on the GUI side I got:
var task = window.setInterval("update()", 250);
function update() {
$.getJSON('/json', function(data) {
refresh(data);
});
}
But for some reason, I cannot access to 'http://localhost:8080/index.html/json', where I should be able to see the dummy JSON output.
Could any of you help me with this project, or show a good working example for something similar ?