roar
Loading...
Searching...
No Matches
websocket_base.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <roar/error.hpp>
7
8#include <promise-cpp/promise.hpp>
9#include <boost/beast/ssl/ssl_stream.hpp>
10#include <boost/beast/core/tcp_stream.hpp>
11#include <boost/beast/websocket.hpp>
12#include <boost/beast/websocket/ssl.hpp>
13
14#include <variant>
15
16namespace Roar
17{
18 class WebsocketBase : public std::enable_shared_from_this<WebsocketBase>
19 {
20 public:
21 WebsocketBase(std::variant<Detail::StreamType, boost::beast::ssl_stream<Detail::StreamType>>&& stream);
22 WebsocketBase(std::variant<
23 boost::beast::websocket::stream<boost::beast::ssl_stream<Detail::StreamType>>,
24 boost::beast::websocket::stream<Detail::StreamType>>&& ws);
27 WebsocketBase(WebsocketBase const&) = delete;
29 virtual ~WebsocketBase();
30
31 template <typename FunctionT>
32 auto withStreamDo(FunctionT&& func)
33 {
34 return std::visit(std::forward<FunctionT>(func), ws_);
35 }
36
37 template <typename FunctionT>
38 auto withStreamDo(FunctionT&& func) const
39 {
40 return std::visit(std::forward<FunctionT>(func), ws_);
41 }
42
48 void readMessageMax(std::size_t amount);
49
55 std::size_t readMessageMax() const;
56
62 void binary(bool enable);
63
70 bool binary() const;
71
80 send(std::string message);
81
87 Detail::
88 PromiseTypeBind<Detail::PromiseTypeBindThen<WebsocketReadResult>, Detail::PromiseTypeBindFail<Error const&>>
89 read();
90
97 Detail::
98 PromiseTypeBind<Detail::PromiseTypeBindThen<WebsocketReadResult>, Detail::PromiseTypeBindFail<Error const&>>
99 read_some(std::size_t limit = 4096);
100
106 void autoFragment(bool enable);
107
114 bool autoFragment() const;
115
122 bool close(std::chrono::seconds closeWaitTimeout = std::chrono::seconds{3});
123
124 protected:
125 std::variant<
126 boost::beast::websocket::stream<boost::beast::ssl_stream<Detail::StreamType>>,
127 boost::beast::websocket::stream<Detail::StreamType>>
129 };
130}
Definition websocket_base.hpp:19
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< WebsocketReadResult >, Detail::PromiseTypeBindFail< Error const & > > read()
Reads something from the server.
Definition websocket_base.cpp:78
WebsocketBase & operator=(WebsocketBase &&)=delete
auto withStreamDo(FunctionT &&func) const
Definition websocket_base.hpp:38
bool close(std::chrono::seconds closeWaitTimeout=std::chrono::seconds{3})
Closes the websocket connection.
Definition websocket_base.cpp:143
virtual ~WebsocketBase()
WebsocketBase & operator=(WebsocketBase const &)=delete
std::size_t readMessageMax() const
Returns the maximum incoming message size setting.
Definition websocket_base.cpp:40
WebsocketBase(WebsocketBase &&)=delete
std::variant< boost::beast::websocket::stream< boost::beast::ssl_stream< Detail::StreamType > >, boost::beast::websocket::stream< Detail::StreamType > > ws_
Definition websocket_base.hpp:128
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< WebsocketReadResult >, Detail::PromiseTypeBindFail< Error const & > > read_some(std::size_t limit=4096)
Reads something from the server.
Definition websocket_base.cpp:104
WebsocketBase(WebsocketBase const &)=delete
auto withStreamDo(FunctionT &&func)
Definition websocket_base.hpp:32
bool autoFragment() const
Returns true if the automatic fragmentation option is set.
Definition websocket_base.cpp:136
Detail::PromiseTypeBind< Detail::PromiseTypeBindThen< std::size_t >, Detail::PromiseTypeBindFail< Error const & > > send(std::string message)
Sends a string to the server.
Definition websocket_base.cpp:62
bool binary() const
Returns true if the binary message write option is set.
Definition websocket_base.cpp:54
boost::beast::basic_stream< boost::asio::ip::tcp, boost::asio::any_io_executor, boost::beast::simple_rate_policy > StreamType
Definition stream_type.hpp:11
Definition authorization.hpp:10
Definition promise_compat.hpp:67
Definition promise_compat.hpp:70