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

Commit 444c6e8

Browse files
committed
ws获取事件函数
1 parent fbcaec7 commit 444c6e8

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

include/mirai_bot.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ namespace Cyan
4747
class EXPORTED MiraiBot
4848
{
4949
public:
50-
MiraiBot() :qq_(0), pool_(4), http_client_("localhost", 8080) {}
51-
MiraiBot(const string& host, int port) : qq_(0), pool_(4), http_client_(host, port) {}
50+
MiraiBot() :
51+
qq_(0),
52+
pool_(4),
53+
http_client_("localhost", 8080),
54+
host_("localhost"),
55+
port_(8080) {}
56+
MiraiBot(const string& host, int port) :
57+
qq_(0),
58+
pool_(4),
59+
http_client_(host, port),
60+
host_(host),
61+
port_(port) {}
5262
~MiraiBot()
5363
{
5464
Release();
@@ -117,7 +127,8 @@ namespace Cyan
117127
private:
118128
bool SessionVerify();
119129
bool SessionRelease();
120-
unsigned int FetchEvents(unsigned int count = 10);
130+
unsigned int FetchEvents_HTTP(unsigned int count = 10);
131+
void FetchEvents_WS();
121132
void ProcessEvents(const nlohmann::json& ele);
122133
template<typename T>
123134
inline WeakEvent MakeWeakEvent(const json& json_)
@@ -157,6 +168,8 @@ namespace Cyan
157168
string authKey_;
158169
QQ_t qq_;
159170
string sessionKey_;
171+
string host_;
172+
int port_;
160173
httplib::Client http_client_;
161174
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
162175
ThreadPool pool_;

src/mirai_bot.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "mirai_bot.hpp"
2+
#include "easywsclient.hpp"
3+
#define _SSIZE_T_DEFINED
4+
#include "easywsclient.cpp"
25

36
namespace Cyan
47
{
@@ -571,7 +574,8 @@ namespace Cyan
571574
unsigned count = 0;
572575
try
573576
{
574-
count = FetchEvents(count_per_loop);
577+
FetchEvents_WS();
578+
count = FetchEvents_HTTP(count_per_loop);
575579
}
576580
catch (const std::exception& ex)
577581
{
@@ -656,7 +660,7 @@ namespace Cyan
656660
}
657661

658662

659-
unsigned int MiraiBot::FetchEvents(unsigned int count)
663+
unsigned int MiraiBot::FetchEvents_HTTP(unsigned int count)
660664
{
661665
int received_count = 0;
662666
stringstream api_url;
@@ -699,6 +703,29 @@ namespace Cyan
699703

700704
}
701705

706+
void MiraiBot::FetchEvents_WS()
707+
{
708+
using namespace easywsclient;
709+
stringstream url;
710+
url << "ws://" << host_ << ":" << port_ << "/all?sessionKey=" << sessionKey_;
711+
std::shared_ptr<WebSocket> ws(WebSocket::from_url(url.str()));
712+
if (!ws)
713+
{
714+
throw std::runtime_error("无法建立 WebSocket 连接!");
715+
}
716+
while (ws->getReadyState() != WebSocket::CLOSED)
717+
{
718+
ws->poll();
719+
string eventJsonStr;
720+
ws->dispatch([&](const std::string& message)
721+
{
722+
eventJsonStr = message;
723+
});
724+
json j = json::parse(eventJsonStr);
725+
ProcessEvents(j);
726+
}
727+
}
728+
702729
void MiraiBot::ProcessEvents(const nlohmann::json& ele)
703730
{
704731
string event_name = ele["type"].get<string>();

0 commit comments

Comments
 (0)