23 #ifndef JWT_GAME_SERVER_BASE_CLIENT_HPP
24 #define JWT_GAME_SERVER_BASE_CLIENT_HPP
26 #include <websocketpp/client.hpp>
28 #include <jwt-cpp/jwt.h>
29 #include <spdlog/spdlog.h>
38 using websocketpp::connection_hdl;
43 using std::placeholders::_1;
44 using std::placeholders::_2;
49 using std::lock_guard;
52 template<
typename client_config>
56 using ws_client = websocketpp::client<client_config>;
57 using message_ptr =
typename ws_client::message_ptr;
63 using super = std::runtime_error;
64 explicit client_error(
const std::string& what_arg) noexcept :
66 explicit client_error(
const char* what_arg) noexcept : super(what_arg) {}
72 client() : m_is_running{false}, m_has_failed{false},
73 m_handle_open{[](){}}, m_handle_close{[](){}},
74 m_handle_message{[](
const std::string& s){}}
78 m_client.set_open_handler(bind(&client::on_open,
this,
79 simple_web_game_server::_1));
80 m_client.set_close_handler(bind(&client::on_close,
this,
81 simple_web_game_server::_1));
82 m_client.set_message_handler(
83 bind(&client::on_message,
this, simple_web_game_server::_1,
84 simple_web_game_server::_2)
90 std::function<
void()> of,
91 std::function<
void()> cf,
92 std::function<
void(
const std::string&)> mf
93 ) : m_is_running{false}, m_has_failed{false},
94 m_handle_open{of}, m_handle_close{cf},
99 m_client.set_open_handler(bind(&client::on_open,
this,
100 simple_web_game_server::_1));
101 m_client.set_close_handler(bind(&client::on_close,
this,
102 simple_web_game_server::_1));
103 m_client.set_message_handler(
104 bind(&client::on_message,
this, simple_web_game_server::_1,
105 simple_web_game_server::_2)
110 client{c.m_handle_open, c.m_handle_close, c.m_handle_message} {}
113 void connect(
const std::string& uri,
const std::string& jwt) {
121 websocketpp::lib::error_code ec;
122 m_connection = m_client.get_connection(uri, ec);
124 spdlog::debug(ec.message());
127 m_client.connect(m_connection);
129 m_has_failed =
false;
131 }
catch(std::exception& e) {
132 m_is_running =
false;
134 spdlog::error(
"error with client connection: {}", e.what());
153 spdlog::trace(
"closing client connection");
155 websocketpp::close::status::normal,
156 "client closed connection"
158 }
catch(std::exception& e) {
159 spdlog::error(
"error closing client connection: {}", e.what());
162 throw client_error(
"disconnect called on stopped client");
175 void send(
const std::string& msg) {
180 websocketpp::frame::opcode::text
182 spdlog::debug(
"client sent message: {}", msg);
183 }
catch(std::exception& e) {
184 spdlog::error(
"error sending client message \"{}\": {}", msg,
189 std::string{
"send called on stopped client with message: "} +
200 throw client_error{
"set_open_handler called on running client"};
209 throw client_error{
"set_close_handler called on running client"};
216 m_handle_message = f;
218 throw client_error{
"set_message_handler called on running client"};
223 void on_open(connection_hdl hdl) {
224 spdlog::trace(
"client connection opened");
228 }
catch(std::exception& e) {
229 spdlog::error(
"error in open handler: {}", e.what());
233 void on_close(connection_hdl hdl) {
234 spdlog::trace(
"client connection closed");
235 m_is_running =
false;
238 }
catch(std::exception& e) {
239 spdlog::error(
"error in close handler: {}", e.what());
243 void on_message(connection_hdl hdl, message_ptr msg) {
244 spdlog::trace(
"client received message: {}", msg->get_payload());
246 m_handle_message(msg->get_payload());
247 }
catch(std::exception& e) {
248 spdlog::error(
"error in message handler: {}", e.what());
254 typename ws_client::connection_ptr m_connection;
258 function<void()> m_handle_open;
259 function<void()> m_handle_close;
260 function<void(
const std::string&)> m_handle_message;
The class representing errors with the client.
Definition: client.hpp:61
A simple WebSocket client to connect to an instance of base_server.
Definition: client.hpp:53
void set_close_handler(std::function< void()> f)
Sets the given function to be called when the client disconnects.
Definition: client.hpp:205
void disconnect()
Close the connection to the server.
Definition: client.hpp:150
client()
Constructs the client with empty handler functions.
Definition: client.hpp:72
bool has_failed()
Returns whether the connection has failed.
Definition: client.hpp:145
void set_message_handler(std::function< void(const std::string &)> f)
Sets the given function to be called when the client gets a message.
Definition: client.hpp:214
client(std::function< void()> of, std::function< void()> cf, std::function< void(const std::string &)> mf)
Constructs the client with the given handler functions.
Definition: client.hpp:89
void send(const std::string &msg)
Synchronously send the given string to the server.
Definition: client.hpp:175
bool is_running()
Returns whether the underlying WebSocket client is running.
Definition: client.hpp:140
void connect(const std::string &uri, const std::string &jwt)
Connects to a server at the given URI and sends the given string.
Definition: client.hpp:113
void set_open_handler(std::function< void()> f)
Sets the given function to be called when the client connects.
Definition: client.hpp:196
void reset()
Resets the client so it may connect to a new server.
Definition: client.hpp:167
Definition: base_server.hpp:52