-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
73 lines (67 loc) · 2.42 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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) {
// Supress 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();
}