From c4d3a2cb5e9f80e4f1ac4a8e8d148f3f590773a2 Mon Sep 17 00:00:00 2001 From: Chi Huu Huynh <73843190+Chi-EEE@users.noreply.github.com> Date: Mon, 4 Dec 2023 19:31:27 +0000 Subject: [PATCH] Fix websocket and use DynamicController instead --- backend/src/controllers/DynamicController.hpp | 21 ++++++++++++++++ backend/src/controllers/Room.hpp | 24 ------------------- backend/src/main.cpp | 2 +- raspberry_pi/src/main.cpp | 4 ++-- 4 files changed, 24 insertions(+), 27 deletions(-) create mode 100644 backend/src/controllers/DynamicController.hpp delete mode 100644 backend/src/controllers/Room.hpp diff --git a/backend/src/controllers/DynamicController.hpp b/backend/src/controllers/DynamicController.hpp new file mode 100644 index 00000000..0f59f334 --- /dev/null +++ b/backend/src/controllers/DynamicController.hpp @@ -0,0 +1,21 @@ +#pragma once +#include + +class DynamicController : public drogon::HttpController +{ +public: + METHOD_LIST_BEGIN + ADD_METHOD_TO(DynamicController::load, "/room/{1}", drogon::Get); + METHOD_LIST_END + void load(const drogon::HttpRequestPtr& req, + std::function&& callback, + std::string&& room_name); +}; + +void DynamicController::load(const drogon::HttpRequestPtr& req, + std::function&& callback, + std::string&& arg1) +{ + // The 'room' page is dynamically generated locally, so we don't have a specific file to serve. + callback(drogon::HttpResponse::newFileResponse(drogon::app().getDocumentRoot() + "/dynamic.html")); +} \ No newline at end of file diff --git a/backend/src/controllers/Room.hpp b/backend/src/controllers/Room.hpp deleted file mode 100644 index aaea529a..00000000 --- a/backend/src/controllers/Room.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include - -using namespace drogon; - -class room : public drogon::HttpController -{ -public: - METHOD_LIST_BEGIN - METHOD_ADD(room::load, "/{1}", Get); - METHOD_LIST_END - void load(const HttpRequestPtr& req, - std::function&& callback, - std::string&& room_name); -}; - -void room::load(const HttpRequestPtr& req, - std::function&& callback, - std::string&& room_name) -{ - // The 'room' page is dynamically generated locally, so we don't have a specific file to serve. - callback(drogon::HttpResponse::newFileResponse(drogon::app().getDocumentRoot() + "/dynamic.html")); -} \ No newline at end of file diff --git a/backend/src/main.cpp b/backend/src/main.cpp index c86b7c11..cb7a3957 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -4,7 +4,7 @@ #include // Controllers must be included before main -#include "controllers/Room.hpp" +#include "controllers/DynamicController.hpp" #include "controllers/WebSocketChat.hpp" int main() diff --git a/raspberry_pi/src/main.cpp b/raspberry_pi/src/main.cpp index 20ff8b40..b17e8463 100644 --- a/raspberry_pi/src/main.cpp +++ b/raspberry_pi/src/main.cpp @@ -30,9 +30,9 @@ std::string get_websocket_url() std::optional maybe_port = GET_CONFIG_VALUE(port); if (maybe_port.has_value()) { - return "ws://" + GET_CONFIG_VALUE(host) + ":" + std::to_string(maybe_port.value()) + "/room?room_name=" + GET_CONFIG_VALUE(code); + return "ws://" + GET_CONFIG_VALUE(host) + ":" + std::to_string(maybe_port.value()) + "/ws/room?room_name=" + GET_CONFIG_VALUE(code); } - return "ws://" + GET_CONFIG_VALUE(host) + "/room/" + GET_CONFIG_VALUE(code); + return "ws://" + GET_CONFIG_VALUE(host) + "/ws/room?room_name=" + GET_CONFIG_VALUE(code); } // Car is a global variable so that car.terminate() can be called on exit