example_chat.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "crow.h"
  2. #include <string>
  3. #include <vector>
  4. #include <chrono>
  5. using namespace std;
  6. vector<string> msgs;
  7. vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> ress;
  8. void broadcast(const string& msg)
  9. {
  10. msgs.push_back(msg);
  11. crow::json::wvalue x;
  12. x["msgs"][0] = msgs.back();
  13. x["last"] = msgs.size();
  14. string body = crow::json::dump(x);
  15. for(auto p : ress)
  16. {
  17. auto* res = p.first;
  18. CROW_LOG_DEBUG << res << " replied: " << body;
  19. res->end(body);
  20. }
  21. ress.clear();
  22. }
  23. // To see how it works go on {ip}:40080 but I just got it working with external build (not directly in IDE, I guess a problem with dependency)
  24. int main()
  25. {
  26. crow::SimpleApp app;
  27. crow::mustache::set_base(".");
  28. CROW_ROUTE(app, "/")
  29. ([]{
  30. crow::mustache::context ctx;
  31. return crow::mustache::load("example_chat.html").render();
  32. });
  33. CROW_ROUTE(app, "/logs")
  34. ([]{
  35. CROW_LOG_INFO << "logs requested";
  36. crow::json::wvalue x;
  37. int start = max(0, (int)msgs.size()-100);
  38. for(int i = start; i < (int)msgs.size(); i++)
  39. x["msgs"][i-start] = msgs[i];
  40. x["last"] = msgs.size();
  41. CROW_LOG_INFO << "logs completed";
  42. return x;
  43. });
  44. CROW_ROUTE(app, "/logs/<int>")
  45. ([](const crow::request& /*req*/, crow::response& res, int after){
  46. CROW_LOG_INFO << "logs with last " << after;
  47. if (after < (int)msgs.size())
  48. {
  49. crow::json::wvalue x;
  50. for(int i = after; i < (int)msgs.size(); i ++)
  51. x["msgs"][i-after] = msgs[i];
  52. x["last"] = msgs.size();
  53. res.write(crow::json::dump(x));
  54. res.end();
  55. }
  56. else
  57. {
  58. vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> filtered;
  59. for(auto p : ress)
  60. {
  61. if (p.first->is_alive() && chrono::steady_clock::now() - p.second < chrono::seconds(30))
  62. filtered.push_back(p);
  63. else
  64. p.first->end();
  65. }
  66. ress.swap(filtered);
  67. ress.push_back({&res, chrono::steady_clock::now()});
  68. CROW_LOG_DEBUG << &res << " stored " << ress.size();
  69. }
  70. });
  71. CROW_ROUTE(app, "/send")
  72. .methods("GET"_method, "POST"_method)
  73. ([](const crow::request& req)
  74. {
  75. CROW_LOG_INFO << "msg from client: " << req.body;
  76. broadcast(req.body);
  77. return "";
  78. });
  79. app.port(40080)
  80. //.multithreaded()
  81. .run();
  82. }