web socket example test #9
@ -90,8 +90,21 @@ endif()
|
|||||||
|
|
||||||
# Find packages go here.
|
# Find packages go here.
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
|
|
||||||
pkg_check_modules(GTK4 REQUIRED gtk4>=4.0)
|
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)
|
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
|
# Setup CMake to use GTK+, tell the compiler where to look for headers
|
||||||
# and to the linker where to look for libraries
|
# and to the linker where to look for libraries
|
||||||
|
@ -5,7 +5,7 @@ This is a Drogon Content Management System(CMS).
|
|||||||
* GPL-3.0
|
* GPL-3.0
|
||||||
|
|
||||||
## Features and Strengths
|
## 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 CMake/C/C++/GTK4/Drogon
|
||||||
- Built on Freedom Software
|
- Built on Freedom Software
|
||||||
- Blazing Fast Website Architecture
|
- Blazing Fast Website Architecture
|
||||||
|
1
vendors/drogon/local-api-server/config.yaml
vendored
1
vendors/drogon/local-api-server/config.yaml
vendored
@ -133,6 +133,7 @@ app:
|
|||||||
- apk
|
- apk
|
||||||
- cur
|
- cur
|
||||||
- xml
|
- xml
|
||||||
|
- webp
|
||||||
# mime: A dictionary that extends the internal MIME type support. Maps extensions into new MIME types
|
# 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.
|
# note: This option only adds MIME to the sever. `file_types` above have to be set for the server to serve them.
|
||||||
mime: {
|
mime: {
|
||||||
|
82
vendors/drogon/local-api-server/main.cc
vendored
82
vendors/drogon/local-api-server/main.cc
vendored
@ -1,11 +1,73 @@
|
|||||||
#include <drogon/drogon.h>
|
#include <drogon/WebSocketController.h>
|
||||||
int main() {
|
#include <drogon/PubSubService.h>
|
||||||
//Set HTTP listener address and port
|
#include <drogon/HttpAppFramework.h>
|
||||||
drogon::app().addListener("0.0.0.0", 7557);
|
using namespace drogon;
|
||||||
//Load config file
|
|
||||||
//drogon::app().loadConfigFile("../config.json");
|
class WebSocketChat : public drogon::WebSocketController<WebSocketChat>
|
||||||
drogon::app().loadConfigFile("../config.yaml");
|
{
|
||||||
//Run HTTP framework,the method will block in the internal event loop
|
public:
|
||||||
drogon::app().run();
|
virtual void handleNewMessage(const WebSocketConnectionPtr &,
|
||||||
return 0;
|
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();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user