wxThread::ExitCode SendStatsThread::Entry() { std::cout << m_url << std::endl; struct mg_mgr mgr; struct mg_connection *nc; mg_mgr_init(&mgr, this); std::string user_agent = "User-Agent: " + wxGetOsDescription().ToStdString() + "\r\n"; std::cout << user_agent << std::endl; nc = mg_connect_http(&mgr, SendStatsThread::ev_handler, m_url.c_str(), user_agent.c_str(), NULL); // GET if (nc != nullptr) { mg_set_protocol_http_websocket(nc); time_t ts_start = time(NULL); time_t ts_end = ts_start; this->m_end = false; while (!this->m_end) { if ((ts_end - ts_start) >= 1) // 1 sec { std::cout << "timeout" << std::endl; break; } ts_end = mg_mgr_poll(&mgr, 1000); } } mg_mgr_free(&mgr); return nullptr; }
int main(int argc, char *argv[]) { struct mg_mgr mgr; int i; mg_mgr_init(&mgr, NULL); /* Process command line arguments */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], s_show_headers_opt) == 0) { s_show_headers = 1; } else if (strcmp(argv[i], "--hexdump") == 0 && i + 1 < argc) { mgr.hexdump_file = argv[++i]; } else { break; } } if (i + 1 != argc) { fprintf(stderr, "Usage: %s [%s] [--hexdump <file>] <URL>\n", argv[0], s_show_headers_opt); exit(EXIT_FAILURE); } mg_connect_http(&mgr, ev_handler, argv[i], NULL, NULL); while (s_exit_flag == 0) { mg_mgr_poll(&mgr, 1000); } mg_mgr_free(&mgr); return 0; }
void send_req(void *arg) { struct mg_connection *nc = mg_connect_http(&s_mgr, mg_cb, "http://www.example.com", NULL, NULL); (void) mg_cb; // struct mg_connection *nc = mg_connect( // &s_mgr, "tcp://www.example.com:80", mg_cb2); DBG(("nc = %p", nc)); }
int main(void) { struct mg_mgr mgr; struct mg_connection *nc; mg_mgr_init(&mgr, NULL); nc = mg_connect_http(&mgr, ev_handler, s_url, NULL, NULL); mg_set_protocol_http_websocket(nc); printf("Starting RESTful client against %s\n", s_url); while (s_exit_flag == 0) { mg_mgr_poll(&mgr, 1000); } mg_mgr_free(&mgr); return 0; }
// 发送一次请求,并回调处理,然后关闭本次连接 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); printf("Send http request %s\n", url.c_str()); // loop while (s_exit_flag == 0) mg_mgr_poll(&mgr, 500); mg_mgr_free(&mgr); }