CROW_WEBSOCKET_ROUTE not found
Hi,
I'm trying to build the example and I'm having an issue with the websocket. I downloaded the latest release version of crow. I tried to compile with the header only library and the .deb, I have the same issue with both
I don't seem to have any issue to fine the macro CROW_ROUTE however I also tried with and without the
#define CROW_MAIN
this is my code:
// #include "crow.h"
#include "logging/Logger.hpp"
#include <unordered_set>
#include <mutex>
#include "third_party/crow/crow_all.h"
#define CROW_MAIN
int main()
{
crow::SimpleApp app;
std::mutex mtx;
std::unordered_set<crow::websocket::connection*> users;
CROW_WEBSOCKET_ROUTE(app, "/ws")
.onopen([&](crow::websocket::connection& conn) {
CROW_LOG_INFO << "new websocket connection from " << conn.get_remote_ip();
std::lock_guard<std::mutex> _(mtx);
users.insert(&conn);
})
.onclose([&](crow::websocket::connection& conn, const std::string& reason) {
CROW_LOG_INFO << "websocket connection closed: " << reason;
std::lock_guard<std::mutex> _(mtx);
users.erase(&conn);
})
.onmessage([&](crow::websocket::connection& /*conn*/, const std::string& data, bool is_binary) {
std::lock_guard<std::mutex> _(mtx);
for (auto u : users)
if (is_binary)
u->send_binary(data);
else
u->send_text(data);
});
CROW_ROUTE(app, "/")
([] {
char name[256];
gethostname(name, 256);
crow::mustache::context x;
x["servername"] = name;
auto page = crow::mustache::load("ws.html");
return page.render(x);
});
logger.info("Starting the webserver");
app.port(40080)
.multithreaded()
.run();
logger.info("Exiting application");
}
this is the error I have
/home/ori/play-ground/src/demo_application/web_server.cpp: In function ‘int main()’:
/home/ori/play-ground/src/demo_application/web_server.cpp:15:5: error: ‘CROW_WEBSOCKET_ROUTE’ was not declared in this scope
15 | CROW_WEBSOCKET_ROUTE(app, "/ws")
| ^~~~~~~~~~~~~~~~~~~~
make[3]: *** [CMakeFiles/web_server.dir/build.make:76: CMakeFiles/web_server.dir/src/demo_application/web_server.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:224: CMakeFiles/web_server.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:231: CMakeFiles/web_server.dir/rule] Error 2
By looking at master I can see that
#define CROW_WEBSOCKET_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url).websocket<decltype(app)>(&app)
is define in app.h
however it seems like the macro has been removed from app.h in the latest release. Is this normal?
Hi!
You can use CROW_ROUTE(app, url).websocket() instead.
Actually, the macro seems to be missing from all recent releases 😮 depsite being in the master branch. Let's ask @The-EDev whether this is intentional.
thanks so much for your help, your trick seems to do the job :)
I'll leave this ticket open, as we might want to put the macro back instead of using that work around so that the framework complies to the documentation
Sorry for the late response, Yes this is intentional, Version 1.0 was released in March and the macro was added in may. All subsequent versions were patches to v1.0 and added no new feature. Historically the macro would be available in v1.1.
The idea is to have releases like v1.0 be stable and bug free, while keeping all the new untested features in master.
Regarding the documentation, there should be a version selection box at the top, the v1.0 option should be the one to pick for that version, since the master option is relevant to that branch.