Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
abedra committed Jan 13, 2024
1 parent 207f3a5 commit a4fe5d8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 119 deletions.
12 changes: 6 additions & 6 deletions example/bin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <sysexits.h>
#include <chrono>
#include <thread>
#include <atomic>

#include "Types.h"
#include "WebSocketClient.h"

std::atomic<bool> continueRunning = true;
Expand All @@ -25,19 +25,19 @@ int main() {

constexpr int FRAME_SIZE = 1024;

WebSocketClient<FRAME_SIZE, WorkflowResult> webSocketClient{
ExecutionContext{"localhost", 8080, "/"},
[](const auto& result) { return WorkflowResult{result}; }
WebSocketClient<FRAME_SIZE> webSocketClient{
SimpleWebSocket::ExecutionContext{"localhost", 8080, "/"},
[](const auto& result) { return SimpleWebSocket::WorkflowResult{result}; }
};

Workflow workflow{
SimpleWebSocket::Workflow workflow{
[&, client = std::move(webSocketClient)]() {
return client.start(continueRunning);
},
[](const auto&) {
std::cout << "Workflow completed successfully" << std::endl;
},
[](const Failure &failure) {
[](const SimpleWebSocket::Failure &failure) {
std::cout << failure << std::endl;
std::this_thread::sleep_for(1s);
}
Expand Down
2 changes: 1 addition & 1 deletion example/cmake/poco.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FetchContent_Declare(
poco
GIT_SHALLOW TRUE
GIT_REPOSITORY "https://github.com/pocoproject/poco.git"
GIT_TAG "poco-1.11.0-release"
GIT_TAG "poco-1.12.5-release"
)
FetchContent_MakeAvailable(poco)
FetchContent_GetProperties(poco)
Expand Down
2 changes: 1 addition & 1 deletion example/cmake/simple_websocket.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FetchContent_Declare(
simple_websocket
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/abedra/simple_websocket
GIT_TAG v0.0.6
GIT_TAG v0.0.10
CONFIGURE_COMMAND ""
BUILD_COMMAND "")
FetchContent_GetProperties(simple_websocket)
Expand Down
102 changes: 0 additions & 102 deletions example/src/Types.h

This file was deleted.

18 changes: 9 additions & 9 deletions example/src/WebSocketClient.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once

#include "Types.h"
#include <simple_websocket.hpp>
#include "ExampleFrameParser.h"

template<int FRAME_SIZE, class A>
template<int FRAME_SIZE>
struct WebSocketClient final {
explicit WebSocketClient(ExecutionContext executionContext,
std::function<A(const std::variant<Failure, std::monostate>&)> resultFn)
explicit WebSocketClient(SimpleWebSocket::ExecutionContext executionContext,
std::function<SimpleWebSocket::WorkflowResult(const std::variant<SimpleWebSocket::Failure, std::monostate>&)> resultFn)
: executionContext_(std::move(executionContext))
, resultFn_(resultFn)
{ }

[[nodiscard]] A start(std::atomic<bool> &continueRunning) const {
[[nodiscard]] SimpleWebSocket::WorkflowResult start(std::atomic<bool> &continueRunning) const {
ExampleFrameParser frameParser;
SimpleWebSocket::MessageParser<std::variant<std::monostate, std::string>> parser{
std::make_unique<ExampleFrameParser>(frameParser)
Expand All @@ -31,19 +31,19 @@ struct WebSocketClient final {
SimpleWebSocket::Message message = SimpleWebSocket::Poco::fromPoco(flags, result.data(), static_cast<int>(result.size()));
const std::variant<std::monostate, std::string> &parseResult = parser.parse(message);
if (std::holds_alternative<std::monostate>(parseResult)) {
return resultFn_(Failure{"WebSocket connection closed"});
return resultFn_(SimpleWebSocket::Failure{"WebSocket connection closed"});
}

std::cout << std::get<std::string>(parseResult) << std::endl;
}
} catch (const std::exception &e) {
return resultFn_(Failure{e.what()});
return resultFn_(SimpleWebSocket::Failure{e.what()});
}

return resultFn_(std::monostate());
}

private:
ExecutionContext executionContext_;
std::function<A(const std::variant<Failure, std::monostate>&)> resultFn_;
SimpleWebSocket::ExecutionContext executionContext_;
std::function<SimpleWebSocket::WorkflowResult(const std::variant<SimpleWebSocket::Failure, std::monostate>&)> resultFn_;
};

0 comments on commit a4fe5d8

Please sign in to comment.