Enabling JSON communication with HTML GUI

202 views
Skip to first unread message

Cyril Albrecht

unread,
May 29, 2018, 10:50:36 AM5/29/18
to civetweb
Hello there,

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.

Here is what I got for now:

        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 ?

bel

unread,
May 30, 2018, 2:24:31 AM5/30/18
to Cyril Albrecht, civetweb
Did you already have a look at this example:

It's not yet complete ... I have to clean the html/js demo script before I add it to the repository.
Bbut the server side will already send you a JSON response.


--
Sourceforge
https://sourceforge.net/projects/civetweb/
---
You received this message because you are subscribed to the Google Groups "civetweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to civetweb+unsubscribe@googlegroups.com.
To post to this group, send email to cive...@googlegroups.com.
Visit this group at https://groups.google.com/group/civetweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/civetweb/4e738989-4d2e-4406-a740-cd69cf3e61f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cyril Albrecht

unread,
May 31, 2018, 11:03:28 AM5/31/18
to civetweb
Hi bel, thanks for the quick anser.

After discussion with my professor I got a working implementation for Linux. After removing some parts which were not compatible with Windows, I could make it work properly on my computer.
Reply all
Reply to author
Forward
0 new messages