Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connection listener to check when the server is down #433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ namespace sio
if(m_socket_open_listener)m_socket_open_listener(nsp);
}

void client_impl::on_client_disconnect(bool const& wasDisconnected)
{
if(m_connection_listener)m_connection_listener(wasDisconnected);
}

/*************************private:*************************/
void client_impl::run_loop()
{
Expand Down
6 changes: 6 additions & 0 deletions src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace sio
SYNTHESIS_SETTER(client::socket_listener,socket_open_listener)

SYNTHESIS_SETTER(client::socket_listener,socket_close_listener)

SYNTHESIS_SETTER(client::connection_listener,connection_listener)

#undef SYNTHESIS_SETTER

Expand All @@ -95,6 +97,7 @@ namespace sio
m_fail_listener = nullptr;
m_reconnect_listener = nullptr;
m_reconnecting_listener = nullptr;
m_connection_listener = nullptr;
}

void clear_socket_listeners()
Expand Down Expand Up @@ -142,6 +145,8 @@ namespace sio
void on_socket_closed(std::string const& nsp);

void on_socket_opened(std::string const& nsp);

void on_client_disconnect(bool const& wasDisconnected);

private:
void run_loop();
Expand Down Expand Up @@ -227,6 +232,7 @@ namespace sio
client::con_listener m_reconnecting_listener;
client::reconnect_listener m_reconnect_listener;
client::close_listener m_close_listener;
client::connection_listener m_connection_listener;

client::socket_listener m_socket_open_listener;
client::socket_listener m_socket_close_listener;
Expand Down
5 changes: 5 additions & 0 deletions src/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ namespace sio
{
m_impl->set_socket_close_listener(l);
}

void client::set_connection_listener(connection_listener const& l)
{
m_impl->set_connection_listener(l);
}

void client::clear_con_listeners()
{
Expand Down
4 changes: 4 additions & 0 deletions src/sio_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace sio
close_reason_normal,
close_reason_drop
};

typedef std::function<void(bool)> connection_listener;

typedef std::function<void(void)> con_listener;

Expand Down Expand Up @@ -57,6 +59,8 @@ namespace sio
void set_socket_open_listener(socket_listener const& l);

void set_socket_close_listener(socket_listener const& l);

void set_connection_listener(connection_listener const& l);

void clear_con_listeners();

Expand Down
2 changes: 2 additions & 0 deletions src/sio_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ namespace sio
m_connection_timer->cancel();
m_connection_timer.reset();
}

m_connected = false;
{
std::lock_guard<std::mutex> guard(m_packet_mutex);
Expand All @@ -377,6 +378,7 @@ namespace sio
while (!m_packet_queue.empty()) {
m_packet_queue.pop();
}
m_client->on_client_disconnect(m_connected);
}
}

Expand Down