-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcc_test.cpp
116 lines (108 loc) · 3.35 KB
/
jcc_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "httplib.h"
#include "json.h"
#include "jcc.h"
/// simplest example ever
void example_HelloWorld() {
jcc::LocalServer sr;
jcc::Html h;
h << "Hello world!";
sr.open(h);
sr.wait();
}
/// eval in browser example
void example_eval() {
jcc::LocalServer sr;
jcc::Html h;
h << "<body>Press CTRL SHIFT I, see the console.</body>";
sr.open(h);
sr.eval() << "console.log('Hello from the c++ code!')" << sr;
sr.wait();
}
/// dom access from the c++
void example_dom() {
jcc::LocalServer sr;
jcc::Html h;
h << "<body><h1 id=\"main\"><h1></body>";
sr.open(h);
for(int i=0;sr.alive();i++){
sr.el("main") << "Seconds passed: <b>" << std::to_string(i) << "</b>" << sr;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
sr.stopGracefully();
}
/// console in browser example
void example_console() {
jcc::LocalServer sr;
jcc::Html h;
h << "<body>Press CTRL SHIFT I, see the console.</body>";
sr.open(h);
sr.console() << "Hello from the c++ code!'" << sr;
sr.wait();
}
/// we get pattern.html, replacing [[NAME]] on "Andrew" and opening the modified html
void example_HtmlPattern() {
jcc::LocalServer sr;
/// if you read by relative path, you need to pass servr reference as well
jcc::Html h("examples/pattern.html", sr);
h.Replace("[[NAME]]", "Andrew");
sr.open(h);
sr.wait();
}
/// we get test_request.html, it sends request using sendObject (defined in main.js), we are modifying object and returning the resulting js object.
void example_ObjectsExchange() {
jcc::LocalServer sr;
sr.exchange([](const json::JSON& obj)->json::JSON {
json::JSON js;
js["User"] = obj;
return js;
});
jcc::Html h("examples/test_request.html");
sr.open(h);
sr.wait();
}
/// The first real example. Let you have ID <=> String correspondence in your app. And you want to edit/translate the text in the browser.
/// In this case the form helps to translate on Japanese language. The example opens page, waits for "Submit", then closes the page.
void example_Translate() {
jcc::LocalServer sr;
json::JSON txt=json::Object();
const char* tl = "ja";
int idx = 0;
auto tag = [&](const char* id, const char* value) {
txt[id] = value;
};
auto add = [&](const char* id, const char* text) {
std::string s = "TextItem" + std::to_string(idx++);
txt[s] = json::Object();
txt[s]["ID"] = id;
txt[s]["Text"] = text;
txt[s]["English"] = text;
txt[s]["tl"] = tl;
};
txt["Language"] = "Japanese";
txt["author"] = "Andrew";
txt["mail"] = "andrewshpagin@gmail.com";
txt["author"] = "Andrew";
add("HELLO", "Hello world!");
add("JCC", "js and c++ interface.");
txt["Button"] = "Submit";
jcc::Html h("examples/edittext.html", sr);
h.Replace("{JSONTEXT}", txt.dump().c_str());
///pay attention, if the form has action <form action="/submit" method="get"> then the result of submitting will be passed there as json object, look the edittext.html
sr.get([&](const jcc::Request& req, jcc::Response& res) {
printf("The translation result:\n%s", req.paramsToJson().dump().c_str());
res = "<html><body><div>Text accepted! Please close the page if it is not closed automatically.</div><script>window.close();</script></body></html>";
sr.signalToStop();
}, "/submit");
sr.open(h);
sr.wait();
}
int main(){
//example_HelloWorld();
example_eval();
//example_console();
//example_dom();
//example_HtmlPattern();
//example_ObjectsExchange();
//example_Translate();
return 0;
}