web socket example test #9

Merged
sharpetronics merged 1 commits from drogonCMS-cmake into master 2024-01-21 18:41:06 -05:00
4 changed files with 87 additions and 11 deletions
Showing only changes of commit 3525c55e81 - Show all commits

View File

@ -90,8 +90,21 @@ endif()
# Find packages go here.
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED gtk4>=4.0)
message("GTK4 include dir: ${GTK4_INCLUDE_DIRS}")
message("GTK4 libraries: ${GTK4_LIBRARY_DIRS}")
message("GTK4 Other CFlags: ${GTK4_CFLAGS_OTHER}")
pkg_check_modules(ADW REQUIRED libadwaita-1>=1.3.5)
message("ADW include dir: ${ADW_INCLUDE_DIRS}")
message("ADW libraries: ${ADW_LIBRARY_DIRS}")
message("ADW Other CFlags: ${ADW_CFLAGS_OTHER}")
#pkg_check_modules(SSL REQUIRED openssl>=3.1.2)
#message("SSL libraries: ${SSL_LIBRARY_DIRS}")
#pkg_check_modules(SSL REQUIRED zlib>=1.3)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries

View File

@ -5,7 +5,7 @@ This is a Drogon Content Management System(CMS).
* GPL-3.0
## Features and Strengths
- Faster than NginX or WordPress and Strapi
- Faster than NginX, WordPress and Strapi
- Built on CMake/C/C++/GTK4/Drogon
- Built on Freedom Software
- Blazing Fast Website Architecture

View File

@ -133,6 +133,7 @@ app:
- apk
- cur
- xml
- webp
# mime: A dictionary that extends the internal MIME type support. Maps extensions into new MIME types
# note: This option only adds MIME to the sever. `file_types` above have to be set for the server to serve them.
mime: {

View File

@ -1,11 +1,73 @@
#include <drogon/drogon.h>
int main() {
//Set HTTP listener address and port
drogon::app().addListener("0.0.0.0", 7557);
//Load config file
//drogon::app().loadConfigFile("../config.json");
drogon::app().loadConfigFile("../config.yaml");
//Run HTTP framework,the method will block in the internal event loop
drogon::app().run();
return 0;
#include <drogon/WebSocketController.h>
#include <drogon/PubSubService.h>
#include <drogon/HttpAppFramework.h>
using namespace drogon;
class WebSocketChat : public drogon::WebSocketController<WebSocketChat>
{
public:
virtual void handleNewMessage(const WebSocketConnectionPtr &,
std::string &&,
const WebSocketMessageType &) override;
virtual void handleConnectionClosed(
const WebSocketConnectionPtr &) override;
virtual void handleNewConnection(const HttpRequestPtr &,
const WebSocketConnectionPtr &) override;
WS_PATH_LIST_BEGIN
WS_PATH_ADD("/chat", Get);
WS_PATH_LIST_END
private:
PubSubService<std::string> chatRooms_;
};
struct Subscriber
{
std::string chatRoomName_;
drogon::SubscriberID id_;
};
void WebSocketChat::handleNewMessage(const WebSocketConnectionPtr &wsConnPtr,
std::string &&message,
const WebSocketMessageType &type)
{
// write your application logic here
LOG_DEBUG << "new websocket message:" << message;
if (type == WebSocketMessageType::Ping)
{
LOG_DEBUG << "recv a ping";
}
else if (type == WebSocketMessageType::Text)
{
auto &s = wsConnPtr->getContextRef<Subscriber>();
chatRooms_.publish(s.chatRoomName_, message);
}
}
void WebSocketChat::handleConnectionClosed(const WebSocketConnectionPtr &conn)
{
LOG_DEBUG << "websocket closed!";
auto &s = conn->getContextRef<Subscriber>();
chatRooms_.unsubscribe(s.chatRoomName_, s.id_);
}
void WebSocketChat::handleNewConnection(const HttpRequestPtr &req,
const WebSocketConnectionPtr &conn)
{
LOG_DEBUG << "new websocket connection!";
conn->send("haha!!!");
Subscriber s;
s.chatRoomName_ = req->getParameter("room_name");
s.id_ = chatRooms_.subscribe(s.chatRoomName_,
[conn](const std::string &topic,
const std::string &message) {
// Suppress unused variable warning
(void)topic;
conn->send(message);
});
conn->setContext(std::make_shared<Subscriber>(std::move(s)));
}
int main()
{
app().addListener("127.0.0.1", 8848).run();
}