Skip to content

Commit

Permalink
Merge pull request #2 from KikoIshimoto/develop
Browse files Browse the repository at this point in the history
websocketのasync対応
  • Loading branch information
KikoIshimoto authored Aug 8, 2023
2 parents ee34431 + 7aade1e commit 4d456e7
Show file tree
Hide file tree
Showing 5 changed files with 550 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ set(CMAKE_CXX_STANDARD 11)
project(savanna VERSION 0.0.13 DESCRIPTION "boost-beast wrapper")

find_package(Boost 1.70.0 REQUIRED COMPONENTS system)
find_package(OpenSSL REQUIRED)
if(Boost_FOUND)
add_library(savanna INTERFACE)
include_directories(${Boost_ROOT} ${Boost_INCLUDE_DIR})
include_directories(${Boost_ROOT} ${Boost_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR})

target_include_directories(savanna INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

Expand All @@ -23,4 +24,5 @@ if(Boost_FOUND)
add_subdirectory(example/async_client)
add_subdirectory(example/reuse_async_client)
add_subdirectory(example/websocket)
add_subdirectory(example/async_websocket)
endif()
19 changes: 19 additions & 0 deletions example/async_websocket/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.15.0)

project(simple_async_ws)
set(CMAKE_CXX_STANDARD 11)

find_package(Threads REQUIRED)

find_package(OpenSSL REQUIRED)
if (OpenSSL_FOUND)
add_executable(simple_async_ws main.cpp)
target_link_libraries(
simple_async_ws
savanna
${OPENSSL_CRYPTO_LIBRARY}
${OPENSSL_SSL_LIBRARY}
Threads::Threads
)
target_include_directories(simple_async_ws INTERFACE ../../include)
endif()
62 changes: 62 additions & 0 deletions example/async_websocket/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <chrono>
#include <iostream>
#include <savanna.hpp>

#include "../cert.hpp"

using namespace m2d;
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
namespace ssl = net::ssl;
using tcp = net::ip::tcp;

int main(int argc, char *argv[])
{
ssl::context ssl_ctx(ssl::context::tlsv12_client);
std::once_flag once;
std::call_once(once, savanna::load_root_cert, m2d::root_cert(), ssl_ctx);

auto ctx = std::make_shared<net::io_context>();

// savanna::url url("ws://echo.websocket.org");
// auto stream = savanna::websocket::raw_stream(ctx);
// savanna::websocket::session<savanna::websocket::raw_stream> session(std::move(resolver), std::move(stream), url);

savanna::url url("ws://localhost:80");
auto stream = std::make_shared<savanna::async_websocket::raw_stream>(*ctx);
savanna::async_websocket::async_session session;

auto executor = session.prepare(ctx, stream, url);

executor->on_message([](beast::flat_buffer buffer) {
std::cout << "received: " << beast::make_printable(buffer.data()) << std::endl;
});

std::thread t([&]() {
try {
executor->run();
} catch (boost::system::system_error e) {
std::cout << "Error: " << e.what() << std::endl;
}
});
t.detach();

int retry_count = 3;
while (executor->current_state() != savanna::async_websocket::state::connected && retry_count > 0) {
std::cout << "connecting..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
retry_count--;
if (retry_count == 0) {
return -1;
}
}

while (executor->current_state() == savanna::async_websocket::state::connected) {
std::cout << "send: hello" << std::endl;
executor->send("hello");
std::this_thread::sleep_for(std::chrono::seconds(1));
}

return 0;
}
3 changes: 2 additions & 1 deletion include/savanna.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
#include "savanna/url.hpp"
#include "savanna/url_session.hpp"
#include "savanna/util.hpp"
#include "savanna/websocket.hpp"
#include "savanna/websocket.hpp"
#include "savanna/async_websocket.hpp"
Loading

0 comments on commit 4d456e7

Please sign in to comment.