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

Commit 4381062

Browse files
committed
Merge branch 'master' of github.com:cyanray/mirai-cpp
merge
2 parents 41af585 + fbb286c commit 4381062

File tree

9 files changed

+990
-24
lines changed

9 files changed

+990
-24
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
2727
$<INSTALL_INTERFACE:include>
2828
)
2929
target_include_directories(${PROJECT_NAME} PUBLIC
30-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party>
30+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mirai-third-party>
3131
$<INSTALL_INTERFACE:third-party>
3232
)
3333

@@ -63,7 +63,7 @@ if(MIRAI_CPP_INSTALL)
6363
)
6464

6565
install(
66-
DIRECTORY third-party
66+
DIRECTORY mirai-third-party
6767
DESTINATION ${CMAKE_INSTALL_PREFIX}
6868
)
6969

examples/FetchEventsViahHTTP.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
// 使用静态库必须要在引入 mirai.h 前定义这个宏
3+
#define MIRAICPP_STATICLIB
4+
#include <mirai.h>
5+
6+
int main()
7+
{
8+
using namespace std;
9+
using namespace Cyan;
10+
system("chcp 65001");
11+
MiraiBot bot("127.0.0.1", 539);
12+
while (true)
13+
{
14+
try
15+
{
16+
bot.Auth("INITKEY7A3O1a9v", 1589588851_qq);
17+
break;
18+
}
19+
catch (const std::exception& ex)
20+
{
21+
cout << ex.what() << endl;
22+
}
23+
MiraiBot::SleepSeconds(1);
24+
}
25+
cout << "成功登录 bot。" << endl;
26+
27+
bot.OnEventReceived<GroupMessage>(
28+
[&](GroupMessage gm)
29+
{
30+
gm.QuoteReply(gm.MessageChain);
31+
});
32+
33+
bot.OnEventReceived<FriendMessage>(
34+
[&](FriendMessage fm)
35+
{
36+
fm.Reply("你好呀, " + fm.MessageChain);
37+
});
38+
39+
bot.OnEventReceived<TempMessage>(
40+
[&](TempMessage tm)
41+
{
42+
tm.Reply(tm.MessageChain);
43+
});
44+
45+
// 默认使用 WebSocket 拉取事件、消息
46+
// 如果要使用 HTTP 可以在 EventLoop 前执行 UseHTTP
47+
// 记录轮询事件时的错误
48+
bot.UseHTTP().EventLoop([](const char* errMsg)
49+
{
50+
cout << "轮询事件时出错: " << errMsg << endl;
51+
});
52+
53+
// 默认参数是在 cerr 输出错误
54+
// bot.EventLoop();
55+
56+
return 0;
57+
}

include/mirai_bot.hpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,22 @@ 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+
cacheSize_(4096),
57+
ws_enabled_(true) {}
58+
MiraiBot(const string& host, int port) :
59+
qq_(0),
60+
pool_(4),
61+
http_client_(host, port),
62+
host_(host),
63+
port_(port),
64+
cacheSize_(4096),
65+
ws_enabled_(true) {}
5266
~MiraiBot()
5367
{
5468
Release();
@@ -112,12 +126,29 @@ namespace Cyan
112126
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
113127
}
114128

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+
115143
void EventLoop(function<void(const char*)> errLogger = nullptr);
116144

117145
private:
118146
bool SessionVerify();
119147
bool SessionRelease();
120-
unsigned int FetchMessagesAndEvents(unsigned int count = 10);
148+
bool SessionConfigure(int cacheSize, bool enableWebsocket);
149+
unsigned int FetchEvents_HTTP(unsigned int count = 10);
150+
void FetchEvents_WS();
151+
void ProcessEvents(const nlohmann::json& ele);
121152
template<typename T>
122153
inline WeakEvent MakeWeakEvent(const json& json_)
123154
{
@@ -139,7 +170,7 @@ namespace Cyan
139170
}
140171

141172
}
142-
173+
143174
// 因为 httplib 使用 string 来保存文件内容,这里适配一下
144175
inline string ReadFile(const string& filename)
145176
{
@@ -156,6 +187,10 @@ namespace Cyan
156187
string authKey_;
157188
QQ_t qq_;
158189
string sessionKey_;
190+
string host_;
191+
int port_;
192+
int cacheSize_;
193+
bool ws_enabled_;
159194
httplib::Client http_client_;
160195
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
161196
ThreadPool pool_;
File renamed without changes.

0 commit comments

Comments
 (0)