diff options
author | ipknHama <ipknhama@gmail.com> | 2014-08-06 03:54:38 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2014-08-06 04:07:46 +0900 |
commit | a0c93f5b84cc11b30bc6320ac26127832ef8bf7a (patch) | |
tree | dc3a51f019b99bf4b4c62201bf6c183a9fb9fa38 /example_chat.cpp | |
parent | 88cc6079cb2ba9790a2d8bb9a5955006bfcc7a16 (diff) | |
download | crow-a0c93f5b84cc11b30bc6320ac26127832ef8bf7a.tar.gz crow-a0c93f5b84cc11b30bc6320ac26127832ef8bf7a.zip |
long polling implementation complete
change `res handle(req)' into `void handle(req, res)'
connnection::handle is divide into two part:
before and after user handler
Diffstat (limited to 'example_chat.cpp')
-rw-r--r-- | example_chat.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/example_chat.cpp b/example_chat.cpp new file mode 100644 index 0000000..994269e --- /dev/null +++ b/example_chat.cpp @@ -0,0 +1,80 @@ +#include "crow.h" +#include "json.h" +#include "mustache.h" +#include <string> +#include <vector> + +using namespace std; + +vector<string> msgs; +vector<crow::response*> ress; + +void broadcast(const string& msg) +{ + msgs.push_back(msg); + crow::json::wvalue x; + x["msgs"][0] = msgs.back(); + x["last"] = msgs.size(); + string body = crow::json::dump(x); + for(auto* res:ress) + { + CROW_LOG_DEBUG << res->p << " replied: " << body; + res->end(body); + } + ress.clear(); +} + +int main() +{ + crow::App app; + crow::mustache::set_base("."); + + CROW_ROUTE(app, "/") + ([]{ + crow::mustache::context ctx; + return crow::mustache::load("example_chat.html").render(); + }); + + CROW_ROUTE(app, "/logs") + ([]{ + CROW_LOG_INFO << "logs requested"; + crow::json::wvalue x; + for(int i = max(0, (int)msgs.size()-100); i < (int)msgs.size(); i++) + x["msgs"][i] = msgs[i]; + x["last"] = msgs.size(); + CROW_LOG_INFO << "logs completed"; + return x; + }); + + CROW_ROUTE(app, "/logs/<int>") + ([](const crow::request& req, crow::response& res, int after){ + CROW_LOG_INFO << "logs with last " << after; + if (after < (int)msgs.size()) + { + crow::json::wvalue x; + for(int i = after; i < (int)msgs.size(); i ++) + x["msgs"][i-after] = msgs[i]; + x["last"] = msgs.size(); + + res.write(crow::json::dump(x)); + res.end(); + } + else + { + ress.push_back(&res); + CROW_LOG_DEBUG << res.p << " stored"; + } + }); + + CROW_ROUTE(app, "/send") + ([](const crow::request& req) + { + CROW_LOG_INFO << "msg from client: " << req.body; + broadcast(req.body); + return ""; + }); + + app.port(18080) + //.multithreaded() + .run(); +} |