Skip to content

Commit

Permalink
feat: allow for custom io_service (#390) (#391)
Browse files Browse the repository at this point in the history
* feat: allow for custom io_service (#390)

* Keep default constructor

---------

Co-authored-by: Joan Marcè i Igual <jmigual@users.noreply.github.com>
  • Loading branch information
mscofield0 and jmigual authored Jun 26, 2023
1 parent b10474e commit 9066882
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace std;
namespace sio
{
/*************************public:*************************/
client_impl::client_impl() :
client_impl::client_impl(client_options const& options) :
m_ping_interval(0),
m_ping_timeout(0),
m_network_thread(),
Expand All @@ -47,7 +47,11 @@ namespace sio
m_client.set_access_channels(alevel::connect|alevel::disconnect|alevel::app);
#endif
// Initialize the Asio transport policy
m_client.init_asio();
if (options.io_context != nullptr) {
m_client.init_asio(options.io_context);
} else {
m_client.init_asio();
}

// Bind the clients we are using
using std::placeholders::_1;
Expand Down
2 changes: 1 addition & 1 deletion src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace sio
con_closed
};

client_impl();
client_impl(client_options const& options);

~client_impl();

Expand Down
6 changes: 4 additions & 2 deletions src/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ using std::stringstream;

namespace sio
{
client::client():
m_impl(new client_impl())
client::client() : m_impl(new client_impl({})) {}

client::client(client_options const& options):
m_impl(new client_impl(options))
{
}

Expand Down
9 changes: 9 additions & 0 deletions src/sio_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@
#include "sio_message.h"
#include "sio_socket.h"

namespace asio {
class io_context;
}

namespace sio
{
class client_impl;

struct client_options {
asio::io_context* io_context = nullptr;
};

class client {
public:
Expand All @@ -32,6 +40,7 @@ namespace sio
typedef std::function<void(std::string const& nsp)> socket_listener;

client();
client(client_options const& options);
~client();

//set listeners and event bindings.
Expand Down

0 comments on commit 9066882

Please sign in to comment.