MCPcopy Create free account
hub / github.com/acl-dev/acl / websocket_run

Function websocket_run

android/samples/websocket/websocket/src/main/cpp/websocket.cpp:259–324  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

257};
258
259bool websocket_run(const char* addr, void (*callback)(void*, void*, const char*),
260 void* env, void* obj)
261{
262 int conn_timeout = 5, rw_timeout = 5;
263 acl::string host("www.test.com");
264 std::vector<acl::string> name_servers;
265 bool debug = false;
266
267 // 定义 AIO 事件引擎
268 acl::aio_handle handle(acl::ENGINE_KERNEL);
269
270 //////////////////////////////////////////////////////////////////////
271
272 if (name_servers.empty()) {
273 name_servers.push_back("8.8.8.8:53");
274 }
275
276 for (std::vector<acl::string>::const_iterator cit = name_servers.begin();
277 cit != name_servers.end(); ++cit) {
278
279 // 设置 DNS 域名服务器地址
280 handle.set_dns((*cit).c_str(), 5);
281 }
282
283
284 // 开始异步连接远程 WEB 服务器
285 websocket_client* conn = new websocket_client(handle, host, callback, env, obj);
286 if (!conn->open(addr, conn_timeout, rw_timeout)) {
287 log_info("connect %s error\r\n", addr);
288
289 delete conn;
290 return false;
291 }
292
293 (*conn).enable_debug(debug); // 是否启用调试方式
294 conn->unzip_body(true); // 针对 HTTP 自动解压
295
296 // 设置 HTTP 请求头,也可将此过程放在 conn->on_connect() 里
297 acl::http_header& head = conn->request_header();
298 head.set_url("/path?name1&name2")
299 .add_param("name3", "")
300 .add_param("n1", "v1")
301 .add_param("n2", "v2")
302 .add_param("n3", "")
303 .set_content_length(0)
304 .set_host(host)
305 .accept_gzip(true)
306 .set_keep_alive(true);
307
308 acl::string buf;
309 head.build_request(buf);
310 log_info("---------------request header-----------------\r\n");
311 log_info("[%s]\r\n", buf.c_str());
312
313 // 开始 AIO 事件循环过程
314 while (true) {
315 // 如果返回 false 则表示不再继续,需要退出
316 if (!handle.check()) {

Calls 13

beginMethod · 0.80
set_dnsMethod · 0.80
request_headerMethod · 0.80
set_keep_aliveMethod · 0.80
accept_gzipMethod · 0.80
log_infoFunction · 0.70
emptyMethod · 0.45
push_backMethod · 0.45
endMethod · 0.45
c_strMethod · 0.45
openMethod · 0.45
build_requestMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…