Skip to content

Commit

Permalink
Allow connect with query string.
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunz committed Apr 23, 2015
1 parent 0b0f265 commit b06184b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace sio
sync_close();
}

void client_impl::connect(const std::string& uri)
void client_impl::connect(const std::string& uri, const std::map<string,string>& query)
{
if(m_reconn_timer)
{
Expand All @@ -86,8 +86,18 @@ namespace sio
m_con_state = con_opening;
m_base_url = uri;
m_reconn_made = 0;

std::string queryString;
for(std::map<std::string,std::string>::const_iterator it=query.begin();it!=query.end();++it){
queryString.append("&");
queryString.append(it->first);
queryString.append("=");
queryString.append(it->second);
}
m_query_string=queryString;

this->reset_states();
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri));
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,queryString));
m_network_thread.reset(new std::thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle?

}
Expand Down Expand Up @@ -182,17 +192,18 @@ namespace sio
"run loop end");
}

void client_impl::connect_impl(const std::string& uri)
void client_impl::connect_impl(const std::string& uri, const std::string& queryString)
{
do{
websocketpp::uri uo(uri);
std::ostringstream ss;

if (m_sid.size()==0) {
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL);
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
}
else
{
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL);
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
}
lib::error_code ec;
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
Expand Down Expand Up @@ -303,7 +314,7 @@ namespace sio
this->reset_states();
LOG("Reconnecting..."<<std::endl);
if(m_reconnecting_listener) m_reconnecting_listener();
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,m_base_url));
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,m_base_url,m_query_string));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace sio
}

// Client Functions - such as send, etc.
void connect(const std::string& uri);
void connect(const std::string& uri, const std::map<string, string>& queryString);

sio::socket::ptr const& socket(const std::string& nsp);

Expand Down Expand Up @@ -120,7 +120,7 @@ namespace sio
private:
void run_loop();

void connect_impl(const std::string& uri);
void connect_impl(const std::string& uri, const std::string& query);

void close_impl(close::status::value const& code,std::string const& reason);

Expand Down Expand Up @@ -165,6 +165,7 @@ namespace sio
// Socket.IO server settings
std::string m_sid;
std::string m_base_url;
std::string m_query_string;

unsigned int m_ping_interval;
unsigned int m_ping_timeout;
Expand Down
10 changes: 8 additions & 2 deletions src/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ namespace sio
{
m_impl->clear_socket_listeners();
}

void client::connect(const std::string& uri)
{
m_impl->connect(uri);
const std::map<string,string> query;
m_impl->connect(uri, query);
}

void client::connect(const std::string& uri, const std::map<string,string>& query)
{
m_impl->connect(uri, query);
}

socket::ptr const& client::socket(const std::string& nsp)
Expand Down
2 changes: 2 additions & 0 deletions src/sio_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ namespace sio
// Client Functions - such as send, etc.
void connect(const std::string& uri);

void connect(const std::string& uri, const std::map<string,string>& query);

void set_reconnect_attempts(int attempts);

void set_reconnect_delay(unsigned millis);
Expand Down

0 comments on commit b06184b

Please sign in to comment.