-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
65 lines (56 loc) · 1.58 KB
/
HttpClient.cpp
File metadata and controls
65 lines (56 loc) · 1.58 KB
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
#include "HttpClient.h"
#include "LogWriter.h"
// 初始化client静态变量
int HttpClient::s_exit_flag = 0;
ReqCallback HttpClient::s_req_callback;
auto m_clientlogger = spdlog::daily_logger_mt("HttpClient", "logs/HttpClient.log", 0, 0);
// 客户端的网络请求响应
void HttpClient::OnHttpEvent(mg_connection *connection, int event_type, void *event_data)
{
http_message *hm = (struct http_message *)event_data;
int connect_status;
switch (event_type)
{
case MG_EV_CONNECT:
connect_status = *(int *)event_data;
if (connect_status != 0)
{
m_clientlogger->info("Error connecting to server, error code:{}", connect_status);
s_exit_flag = 1;
}
break;
case MG_EV_HTTP_REPLY:
{
m_clientlogger->info("Got reply:{}{}", (int)hm->body.len, hm->body.p);
std::string rsp = std::string(hm->body.p, hm->body.len);
connection->flags |= MG_F_SEND_AND_CLOSE;
s_exit_flag = 1; // 每次收到请求后关闭本次连接,重置标记
// 回调处理
s_req_callback(rsp);
}
break;
case MG_EV_CLOSE:
if (s_exit_flag == 0)
{
m_clientlogger->info("Server closed connection");
s_exit_flag = 1;
};
break;
default:
break;
}
}
// 发送一次请求,并回调处理,然后关闭本次连接
void HttpClient::SendReq(const std::string &url, ReqCallback req_callback)
{
// 给回调函数赋值
s_req_callback = req_callback;
mg_mgr mgr;
mg_mgr_init(&mgr, NULL);
auto connection = mg_connect_http(&mgr, OnHttpEvent, url.c_str(), NULL, NULL);
mg_set_protocol_http_websocket(connection);
m_clientlogger->info("Send http request {}", url.c_str());
while (s_exit_flag == 0)
mg_mgr_poll(&mgr, 500);
mg_mgr_free(&mgr);
}