Skip to content

Commit f1549c4

Browse files
committed
Fixed #8 added method sendJson
1 parent acdcee2 commit f1549c4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
## Integrate to your project
66

7-
Just include this files:
7+
Include this files:
88

9-
- src/wsjcpp_core/wsjcpp_core.h
10-
- src/wsjcpp_core/wsjcpp_core.cpp
9+
- src.wsjcpp/wsjcpp_core/wsjcpp_core.h
10+
- src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp
11+
- src.wsjcpp/nlohmann_json/json.hpp
1112
- src/wsjcpp_light_web_http_request.h
1213
- src/wsjcpp_light_web_http_request.cpp
1314
- src/wsjcpp_light_web_http_response.h
@@ -50,6 +51,9 @@ Will be good for single-web-app (like angular)
5051

5152
Example init, add handler and start server
5253
```
54+
#include <wsjcpp_light_web_http_handler_rewrite_folder.h>
55+
56+
...
5357
WSJCppLightWebServer httpServer;
5458
httpServer.setPort(1234);
5559
httpServer.setMaxWorkers(1);
@@ -74,6 +78,9 @@ Specific: if file does not exists wil be returned 404 not found
7478

7579
Example init, add handler and start server
7680
```
81+
#include <wsjcpp_light_web_http_handler_web_folder.h>
82+
83+
...
7784
WSJCppLightWebServer httpServer;
7885
httpServer.setPort(1234);
7986
httpServer.setMaxWorkers(1);

src/wsjcpp_light_web_http_response.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ void WSJCppLightWebHttpResponse::sendText(const std::string &sBody) {
174174

175175
// ----------------------------------------------------------------------
176176

177+
void WSJCppLightWebHttpResponse::sendJson(const nlohmann::json &json) {
178+
m_sDataType = "application/json";
179+
std::string sBody = json.dump();
180+
std::string sResponse = prepareHeaders(sBody.length())
181+
+ "\r\n" + sBody;
182+
183+
if (m_bClosed) {
184+
WSJCppLog::warn(TAG, "Already sended response");
185+
return;
186+
}
187+
m_bClosed = true;
188+
189+
WSJCppLog::info(TAG, "\nResponse: \n>>>\n" + sResponse + "\n<<<");
190+
191+
send(m_nSockFd, sResponse.c_str(), sResponse.length(),0);
192+
close(m_nSockFd);
193+
}
194+
195+
// ----------------------------------------------------------------------
196+
177197
void WSJCppLightWebHttpResponse::sendEmpty() {
178198
this->sendText("");
179199
}
@@ -246,4 +266,4 @@ void WSJCppLightWebHttpResponse::sendBuffer(const std::string &sFilePath, const
246266
write(m_nSockFd, sResponse.c_str(), sResponse.length());
247267
write(m_nSockFd, pBuffer, nBufferSize);
248268
close(m_nSockFd);
249-
}
269+
}

src/wsjcpp_light_web_http_response.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <string>
55
#include <map>
6+
#include <json.hpp>
67

78
// ---------------------------------------------------------------------
89

@@ -25,6 +26,7 @@ class WSJCppLightWebHttpResponse {
2526
WSJCppLightWebHttpResponse &cacheSec(int nCacheSec);
2627

2728
void sendText(const std::string &sBody);
29+
void sendJson(const nlohmann::json &json);
2830
void sendEmpty();
2931
void sendOptions(const std::string &sOptions);
3032
void sendFile(const std::string &sFilePath);

0 commit comments

Comments
 (0)