diff --git a/README.md b/README.md index c6544f1a..73b7ccb8 100644 --- a/README.md +++ b/README.md @@ -467,8 +467,8 @@ async_simple::coro::Lazy test_websocket() { std::cout << data.resp_body << std::endl; }); - bool r = co_await client.async_ws_connect("ws://localhost:8090/ws"); - if (!r) { + auto r = co_await client.connect("ws://localhost:8090/ws"); + if (r.net_err) { co_return; } diff --git a/example/main.cpp b/example/main.cpp index 2ec99767..92fc4c4b 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -195,8 +195,8 @@ async_simple::coro::Lazy use_websocket() { std::this_thread::sleep_for(300ms); // wait for server start coro_http_client client{}; - bool r = co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo"); - if (!r) { + auto r = co_await client.connect("ws://127.0.0.1:9001/ws_echo"); + if (r.net_err) { co_return; } diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index a7412bbc..f2ae71b5 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -289,6 +289,21 @@ class coro_http_client : public std::enable_shared_from_this { { auto time_out_guard = timer_guard(this, conn_timeout_duration_, "connect timer"); + if (u.is_websocket()) { + // build websocket http header + add_header("Upgrade", "websocket"); + add_header("Connection", "Upgrade"); + if (ws_sec_key_.empty()) { + ws_sec_key_ = "s//GYHa/XO7Hd2F2eOGfyA=="; // provide a random string. + } + add_header("Sec-WebSocket-Key", ws_sec_key_); + add_header("Sec-WebSocket-Version", "13"); + + req_context<> ctx{}; + data = co_await async_request(std::move(uri), http_method::GET, + std::move(ctx)); + co_return data; + } data = co_await connect(u); } if (socket_->is_timeout_) { @@ -319,31 +334,6 @@ class coro_http_client : public std::enable_shared_from_this { void set_ws_sec_key(std::string sec_key) { ws_sec_key_ = std::move(sec_key); } - async_simple::coro::Lazy async_ws_connect(std::string uri) { - resp_data data{}; - auto [r, u] = handle_uri(data, uri); - if (!r) { - CINATRA_LOG_WARNING << "url error:"; - co_return false; - } - - req_context<> ctx{}; - if (u.is_websocket()) { - // build websocket http header - add_header("Upgrade", "websocket"); - add_header("Connection", "Upgrade"); - if (ws_sec_key_.empty()) { - ws_sec_key_ = "s//GYHa/XO7Hd2F2eOGfyA=="; // provide a random string. - } - add_header("Sec-WebSocket-Key", ws_sec_key_); - add_header("Sec-WebSocket-Version", "13"); - } - - data = co_await async_request(std::move(uri), http_method::GET, - std::move(ctx)); - co_return !data.net_err; - } - async_simple::coro::Lazy read_websocket() { co_return co_await async_read_ws(); } @@ -1779,7 +1769,6 @@ class coro_http_client : public std::enable_shared_from_this { co_return resp_data{{}, 200}; } - // this function must be called before async_ws_connect. async_simple::coro::Lazy async_read_ws() { resp_data data{}; diff --git a/lang/coro_http_client_introduction.md b/lang/coro_http_client_introduction.md index 623b2d7b..65c7091e 100644 --- a/lang/coro_http_client_introduction.md +++ b/lang/coro_http_client_introduction.md @@ -433,7 +433,7 @@ void on_ws_msg(std::function on_ws_msg); ``` websocket 连接服务器接口: ```c++ -async_simple::coro::Lazy async_ws_connect(std::string uri); +async_simple::coro::Lazy connect(std::string uri); ``` websocket 发送数据接口: ```c++ @@ -474,7 +474,7 @@ websocket 例子: coro_http_client client; // 连接websocket 服务器 async_simple::coro::syncAwait( - client.async_ws_connect("ws://localhost:8090")); + client.connect("ws://localhost:8090")); std::string send_str(len, 'a'); // 发送websocket 数据 diff --git a/lang/coroutine_based_http_lib.md b/lang/coroutine_based_http_lib.md index f212c773..cfda7801 100644 --- a/lang/coroutine_based_http_lib.md +++ b/lang/coroutine_based_http_lib.md @@ -205,7 +205,7 @@ client端: cinatra::coro_http_client client{}; std::string message(100, 'x'); - co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo"); + co_await client.connect("ws://127.0.0.1:9001/ws_echo"); co_await client.write_websocket(std::string(message)); auto data = co_await client.read_websocket(); CHECK(data.resp_body == message); diff --git a/lang/english/README.md b/lang/english/README.md index ad5bd990..f9d1d971 100644 --- a/lang/english/README.md +++ b/lang/english/README.md @@ -425,8 +425,8 @@ async_simple::coro::Lazy test_websocket() { std::cout << data.resp_body << std::endl; }); - bool r = co_await client.async_ws_connect("ws://localhost:8090/ws"); - if (!r) { + auto r = co_await client.connect("ws://localhost:8090/ws"); + if (r.net_err) { co_return; } diff --git a/tests/test_cinatra_websocket.cpp b/tests/test_cinatra_websocket.cpp index 6b07de61..124c0816 100644 --- a/tests/test_cinatra_websocket.cpp +++ b/tests/test_cinatra_websocket.cpp @@ -43,8 +43,7 @@ TEST_CASE("test wss client") { "../../include/cinatra/server.crt"); REQUIRE_MESSAGE(ok == true, "init ssl fail, please check ssl config"); - REQUIRE(async_simple::coro::syncAwait( - client.async_ws_connect("wss://localhost:9001"))); + async_simple::coro::syncAwait(client.connect("wss://localhost:9001")); async_simple::coro::syncAwait(client.write_websocket("hello")); auto data = async_simple::coro::syncAwait(client.read_websocket()); @@ -57,8 +56,8 @@ TEST_CASE("test wss client") { #endif async_simple::coro::Lazy test_websocket(coro_http_client &client) { - bool r = co_await client.async_ws_connect("ws://localhost:8090/ws"); - if (!r) { + auto r = co_await client.connect("ws://localhost:8090/ws"); + if (r.net_err) { co_return; } @@ -133,7 +132,7 @@ void test_websocket_content(size_t len) { auto lazy = [len]() -> async_simple::coro::Lazy { coro_http_client client{}; - co_await client.async_ws_connect("ws://localhost:8090"); + co_await client.connect("ws://localhost:8090"); std::string send_str(len, 'a'); co_await client.write_websocket(std::string(send_str)); auto data = co_await client.read_websocket(); @@ -170,8 +169,7 @@ TEST_CASE("test send after server stop") { std::this_thread::sleep_for(std::chrono::milliseconds(300)); coro_http_client client{}; - REQUIRE(async_simple::coro::syncAwait( - client.async_ws_connect("ws://127.0.0.1:8090"))); + async_simple::coro::syncAwait(client.connect("ws://127.0.0.1:8090")); server.stop(); @@ -222,7 +220,7 @@ TEST_CASE("test read write in different threads") { }); auto lazy = [client, weak, &send_str]() -> async_simple::coro::Lazy { - co_await client->async_ws_connect("ws://localhost:8090"); + co_await client->connect("ws://localhost:8090"); for (int i = 0; i < 100; i++) { auto data = co_await client->write_websocket(std::string(send_str)); if (data.net_err) { @@ -240,8 +238,8 @@ TEST_CASE("test read write in different threads") { async_simple::coro::Lazy test_websocket() { coro_http_client client{}; - bool r = co_await client.async_ws_connect("ws://127.0.0.1:8089/ws_echo"); - if (!r) { + auto r = co_await client.connect("ws://127.0.0.1:8089/ws_echo"); + if (r.net_err) { co_return; } diff --git a/tests/test_coro_http_server.cpp b/tests/test_coro_http_server.cpp index 7bf5e275..91dbb481 100644 --- a/tests/test_coro_http_server.cpp +++ b/tests/test_coro_http_server.cpp @@ -825,7 +825,7 @@ TEST_CASE("test websocket with chunked") { coro_http_client client{}; async_simple::coro::syncAwait( - client.async_ws_connect("ws://127.0.0.1:9001/ws_source")); + client.connect("ws://127.0.0.1:9001/ws_source")); std::string filename = "test.tmp"; create_file(filename); @@ -911,7 +911,7 @@ TEST_CASE("test websocket") { auto lazy = []() -> async_simple::coro::Lazy { coro_http_client client{}; - co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo"); + co_await client.connect("ws://127.0.0.1:9001/ws_echo"); co_await client.write_websocket("test2fdsaf", true, opcode::binary); auto data = co_await client.read_websocket(); CHECK(data.resp_body == "test2fdsaf"); @@ -1027,7 +1027,7 @@ TEST_CASE("test websocket binary data") { auto client1 = std::make_shared(); async_simple::coro::syncAwait( - client1->async_ws_connect("ws://127.0.0.1:9001/short_binary")); + client1->connect("ws://127.0.0.1:9001/short_binary")); std::string short_str(127, 'A'); async_simple::coro::syncAwait( @@ -1035,7 +1035,7 @@ TEST_CASE("test websocket binary data") { auto client2 = std::make_shared(); async_simple::coro::syncAwait( - client2->async_ws_connect("ws://127.0.0.1:9001/medium_binary")); + client2->connect("ws://127.0.0.1:9001/medium_binary")); std::string medium_str(65535, 'A'); async_simple::coro::syncAwait( @@ -1043,7 +1043,7 @@ TEST_CASE("test websocket binary data") { auto client3 = std::make_shared(); async_simple::coro::syncAwait( - client3->async_ws_connect("ws://127.0.0.1:9001/long_binary")); + client3->connect("ws://127.0.0.1:9001/long_binary")); std::string long_str(65536, 'A'); async_simple::coro::syncAwait( @@ -1106,7 +1106,7 @@ TEST_CASE("test websocket with different message size") { auto lazy = [](std::string &str) -> async_simple::coro::Lazy { coro_http_client client{}; - co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo1"); + co_await client.connect("ws://127.0.0.1:9001/ws_echo1"); co_await client.write_websocket(str); auto data = co_await client.read_websocket(); CHECK(data.resp_body.size() == str.size());