Skip to content

Commit

Permalink
Merge pull request #104 from a-tatarsky/master
Browse files Browse the repository at this point in the history
client: support custom HTTP headers
  • Loading branch information
melode11 authored Sep 18, 2016
2 parents 52e99b7 + cda7b20 commit 725a8e0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace sio
sync_close();
}

void client_impl::connect(const string& uri, const map<string,string>& query)
void client_impl::connect(const string& uri, const map<string,string>& query, const map<string, string>& headers)
{
if(m_reconn_timer)
{
Expand Down Expand Up @@ -99,6 +99,8 @@ namespace sio
}
m_query_string=move(query_str);

m_http_headers = headers;

this->reset_states();
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,m_query_string));
m_network_thread.reset(new thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle?
Expand Down Expand Up @@ -225,6 +227,10 @@ namespace sio
break;
}

for( auto&& header: m_http_headers ) {
con->replace_header(header.first, header.second);
}

m_client.connect(con);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ namespace sio
}

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

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

Expand Down Expand Up @@ -182,6 +183,7 @@ namespace sio
std::string m_sid;
std::string m_base_url;
std::string m_query_string;
std::map<std::string, std::string> m_http_headers;

unsigned int m_ping_interval;
unsigned int m_ping_timeout;
Expand Down
11 changes: 8 additions & 3 deletions src/sio_client.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ namespace sio

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

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

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

socket::ptr const& client::socket(const std::string& nsp)
Expand Down
3 changes: 3 additions & 0 deletions src/sio_client.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ namespace sio

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

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

void set_reconnect_attempts(int attempts);

void set_reconnect_delay(unsigned millis);
Expand Down

0 comments on commit 725a8e0

Please sign in to comment.