Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 8331d6f

Browse files
committed
配置Session开启WebSocket;
1 parent 444c6e8 commit 8331d6f

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

include/mirai_bot.hpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,21 @@ namespace Cyan
4848
{
4949
public:
5050
MiraiBot() :
51-
qq_(0),
52-
pool_(4),
51+
qq_(0),
52+
pool_(4),
5353
http_client_("localhost", 8080),
5454
host_("localhost"),
55-
port_(8080) {}
56-
MiraiBot(const string& host, int port) :
57-
qq_(0),
58-
pool_(4),
59-
http_client_(host, port),
55+
port_(8080),
56+
cacheSize_(4096),
57+
ws_enabled_(false) {}
58+
MiraiBot(const string& host, int port) :
59+
qq_(0),
60+
pool_(4),
61+
http_client_(host, port),
6062
host_(host),
61-
port_(port) {}
63+
port_(port),
64+
cacheSize_(4096),
65+
ws_enabled_(false) {}
6266
~MiraiBot()
6367
{
6468
Release();
@@ -122,11 +126,26 @@ namespace Cyan
122126
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
123127
}
124128

129+
MiraiBot& UseWebSocket()
130+
{
131+
this->ws_enabled_ = true;
132+
SessionConfigure(cacheSize_, ws_enabled_);
133+
return *this;
134+
}
135+
136+
MiraiBot& UseHTTP()
137+
{
138+
this->ws_enabled_ = false;
139+
SessionConfigure(cacheSize_, ws_enabled_);
140+
return *this;
141+
}
142+
125143
void EventLoop(function<void(const char*)> errLogger = nullptr);
126144

127145
private:
128146
bool SessionVerify();
129147
bool SessionRelease();
148+
bool SessionConfigure(int cacheSize, bool enableWebsocket);
130149
unsigned int FetchEvents_HTTP(unsigned int count = 10);
131150
void FetchEvents_WS();
132151
void ProcessEvents(const nlohmann::json& ele);
@@ -151,7 +170,7 @@ namespace Cyan
151170
}
152171

153172
}
154-
173+
155174
// 因为 httplib 使用 string 来保存文件内容,这里适配一下
156175
inline string ReadFile(const string& filename)
157176
{
@@ -170,6 +189,8 @@ namespace Cyan
170189
string sessionKey_;
171190
string host_;
172191
int port_;
192+
int cacheSize_;
193+
bool ws_enabled_;
173194
httplib::Client http_client_;
174195
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
175196
ThreadPool pool_;

src/mirai_bot.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,16 @@ namespace Cyan
569569
{
570570
const unsigned count_per_loop = 20;
571571
const unsigned time_interval = 100;
572+
SessionConfigure(cacheSize_, ws_enabled_);
572573
while (true)
573574
{
574575
unsigned count = 0;
575576
try
576577
{
577-
FetchEvents_WS();
578-
count = FetchEvents_HTTP(count_per_loop);
578+
if (ws_enabled_)
579+
FetchEvents_WS();
580+
else
581+
count = FetchEvents_HTTP(count_per_loop);
579582
}
580583
catch (const std::exception& ex)
581584
{
@@ -659,6 +662,38 @@ namespace Cyan
659662
return false;
660663
}
661664

665+
bool MiraiBot::SessionConfigure(int cacheSize, bool enableWebsocket)
666+
{
667+
json data =
668+
{
669+
{ "sessionKey", sessionKey_ },
670+
{ "cacheSize", cacheSize },
671+
{ "enableWebsocket", enableWebsocket }
672+
};
673+
674+
auto res = http_client_.Post("/config", data.dump(), "application/json;charset=UTF-8");
675+
if (res)
676+
{
677+
if (res->status != 200)
678+
throw std::runtime_error("[mirai-api-http error]: " + res->body);
679+
json reJson;
680+
reJson = reJson.parse(res->body);
681+
int code = reJson["code"].get<int>();
682+
if (code == 0)
683+
return true;
684+
else
685+
{
686+
string msg = reJson["msg"].get<string>();
687+
throw runtime_error(msg);
688+
}
689+
690+
}
691+
else
692+
throw std::runtime_error("网络错误");
693+
return false;
694+
695+
}
696+
662697

663698
unsigned int MiraiBot::FetchEvents_HTTP(unsigned int count)
664699
{
@@ -713,16 +748,20 @@ namespace Cyan
713748
{
714749
throw std::runtime_error("无法建立 WebSocket 连接!");
715750
}
716-
while (ws->getReadyState() != WebSocket::CLOSED)
751+
string eventJsonStr;
752+
while (ws->getReadyState() != WebSocket::CLOSED && this->ws_enabled_)
717753
{
754+
718755
ws->poll();
719-
string eventJsonStr;
720756
ws->dispatch([&](const std::string& message)
721757
{
722-
eventJsonStr = message;
758+
eventJsonStr = std::move(message);
723759
});
724-
json j = json::parse(eventJsonStr);
725-
ProcessEvents(j);
760+
if (!eventJsonStr.empty())
761+
{
762+
json j = json::parse(eventJsonStr);
763+
ProcessEvents(j);
764+
}
726765
}
727766
}
728767

0 commit comments

Comments
 (0)