Skip to content

Commit 7fcf913

Browse files
committed
Passing uri in websocket client connect instead of in the constructor
1 parent 37b4002 commit 7fcf913

File tree

9 files changed

+158
-131
lines changed

9 files changed

+158
-131
lines changed

Release/include/cpprest/ws_client.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ class _websocket_client_impl
235235
return m_uri;
236236
}
237237

238+
/// <summary>
239+
/// Set the base uri.
240+
/// </summary>
241+
/// <param name="uri"> The user specified uri. </param>
242+
void set_uri(web::uri uri)
243+
{
244+
m_uri = std::move(uri);
245+
}
246+
238247
/// <summary>
239248
/// Get client configuration object
240249
/// </summary>
@@ -266,13 +275,15 @@ class _websocket_client_impl
266275
}
267276
}
268277

278+
269279
protected:
270-
_websocket_client_impl(web::uri address, websocket_client_config client_config)
271-
: m_uri(std::move(address)), m_client_config(std::move(client_config))
280+
web::uri m_uri;
281+
282+
_websocket_client_impl(websocket_client_config client_config)
283+
: m_client_config(std::move(client_config))
272284
{
273285
}
274286

275-
web::uri m_uri;
276287
websocket_client_config m_client_config;
277288
};
278289
}
@@ -284,17 +295,15 @@ class websocket_client
284295
{
285296
public:
286297
/// <summary>
287-
/// Creates a new websocket_client to connect to the specified uri.
298+
/// Creates a new websocket_client.
288299
/// </summary>
289-
/// <param name="base_uri">A string representation of the base uri to be used for all messages. Must start with either "ws://" or "wss://"</param>
290-
_ASYNCRTIMP websocket_client(web::uri base_uri);
300+
_ASYNCRTIMP websocket_client();
291301

292302
/// <summary>
293-
/// Creates a new websocket_client to connect to the specified uri.
303+
/// Creates a new websocket_client.
294304
/// </summary>
295-
/// <param name="base_uri">A string representation of the base uri to be used for all requests. Must start with either "ws://" or "wss://"</param>
296305
/// <param name="client_config">The client configuration object containing the possible configuration options to intitialize the <c>websocket_client</c>. </param>
297-
_ASYNCRTIMP websocket_client(web::uri base_uri, websocket_client_config client_config);
306+
_ASYNCRTIMP websocket_client(websocket_client_config client_config);
298307

299308
/// <summary>
300309
/// </summary>
@@ -324,8 +333,13 @@ class websocket_client
324333
/// Connects to the remote network destination. The connect method initiates the websocket handshake with the
325334
/// remote network destination, takes care of the protocol upgrade request.
326335
/// </summary>
336+
/// <param name="uri">The uri address to connect. </param>
327337
/// <returns>An asynchronous operation that is completed once the client has successfully connected to the websocket server.</returns>
328-
pplx::task<void> connect() { return m_client->connect(); }
338+
pplx::task<void> connect(web::uri uri)
339+
{
340+
m_client->set_uri(std::move(uri));
341+
return m_client->connect();
342+
}
329343

330344
/// <summary>
331345
/// Sends a websocket message to the server .

Release/src/websockets/client/ws_client.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,12 @@ class ws_desktop_client : public _websocket_client_impl, public std::enable_shar
9393
DESTROYED
9494
};
9595
public:
96-
ws_desktop_client(web::uri address,
97-
websocket_client_config client_config)
98-
: _websocket_client_impl(std::move(address), std::move(client_config)),
96+
ws_desktop_client(websocket_client_config client_config)
97+
: _websocket_client_impl(std::move(client_config)),
9998
m_work(new boost::asio::io_service::work(m_service)),
10099
m_state(UNINITIALIZED),
101100
m_scheduled(0)
102101
{
103-
verify_uri(m_uri);
104-
105102
m_client.clear_access_channels(websocketpp::log::alevel::all);
106103
m_client.clear_error_channels(websocketpp::log::alevel::all);
107104

@@ -166,7 +163,7 @@ class ws_desktop_client : public _websocket_client_impl, public std::enable_shar
166163
if (m_state == CONNECTED)
167164
{
168165
m_state = CLOSING;
169-
166+
170167
websocketpp::lib::error_code ec;
171168
m_client.close(m_con, static_cast<websocketpp::close::status::value>(status), utility::conversions::to_utf8string(reason), ec);
172169
if (ec.value() != 0)
@@ -180,6 +177,8 @@ class ws_desktop_client : public _websocket_client_impl, public std::enable_shar
180177

181178
pplx::task<void> connect()
182179
{
180+
verify_uri(m_uri);
181+
183182
_ASSERTE(m_state == INITIALIZED);
184183
m_client.set_open_handler([this](websocketpp::connection_hdl)
185184
{
@@ -530,17 +529,15 @@ class ws_desktop_client : public _websocket_client_impl, public std::enable_shar
530529

531530
}
532531

533-
websocket_client::websocket_client(web::uri base_uri)
534-
:m_client(std::make_shared<details::ws_desktop_client>(
535-
std::move(base_uri), websocket_client_config()))
536-
{
537-
}
532+
websocket_client::websocket_client()
533+
:m_client(std::make_shared<details::ws_desktop_client>(websocket_client_config()))
534+
{
535+
}
538536

539-
websocket_client::websocket_client(web::uri base_uri, websocket_client_config config)
540-
:m_client(std::make_shared<details::ws_desktop_client>(
541-
std::move(base_uri), std::move(config)))
542-
{
543-
}
537+
websocket_client::websocket_client(websocket_client_config config)
538+
: m_client(std::make_shared<details::ws_desktop_client>(std::move(config)))
539+
{
540+
}
544541

545542
}}}}
546543

Release/src/websockets/client/ws_winrt.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ ref class ReceiveContext sealed
7272
class winrt_client : public _websocket_client_impl, public std::enable_shared_from_this<winrt_client>
7373
{
7474
public:
75-
winrt_client(web::uri address, websocket_client_config client_config)
76-
: _websocket_client_impl(std::move(address), std::move(client_config)), m_scheduled(0), m_client_closed(false)
75+
winrt_client(websocket_client_config client_config)
76+
: _websocket_client_impl(std::move(client_config)), m_scheduled(0), m_client_closed(false)
7777
{
78-
verify_uri(m_uri);
7978
m_msg_websocket = ref new MessageWebSocket();
8079

8180
// Sets the HTTP request headers to the HTTP request message used in the WebSocket protocol handshake
@@ -153,6 +152,8 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
153152

154153
pplx::task<void> connect()
155154
{
155+
verify_uri(m_uri);
156+
156157
const auto &proxy = config().proxy();
157158
if(!proxy.is_default())
158159
{
@@ -171,7 +172,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
171172

172173
m_msg_websocket->MessageReceived += ref new TypedEventHandler<MessageWebSocket^, MessageWebSocketMessageReceivedEventArgs^>(m_context, &ReceiveContext::OnReceive);
173174
m_msg_websocket->Closed += ref new TypedEventHandler<IWebSocket^, WebSocketClosedEventArgs^>(m_context, &ReceiveContext::OnClosed);
174-
175+
175176
return pplx::create_task(m_msg_websocket->ConnectAsync(uri)).then([=](pplx::task<void> result) -> pplx::task<void>
176177
{
177178
try
@@ -458,13 +459,13 @@ void ReceiveContext::OnClosed(IWebSocket^ sender, WebSocketClosedEventArgs^ args
458459
}
459460
}
460461

461-
websocket_client::websocket_client(web::uri base_uri)
462-
:m_client(std::make_shared<details::winrt_client>(std::move(base_uri), websocket_client_config()))
462+
websocket_client::websocket_client()
463+
:m_client(std::make_shared<details::winrt_client>(websocket_client_config()))
463464
{
464465
}
465466

466-
websocket_client::websocket_client(web::uri base_uri, websocket_client_config config)
467-
:m_client(std::make_shared<details::winrt_client>(std::move(base_uri), std::move(config)))
467+
websocket_client::websocket_client(websocket_client_config config)
468+
:m_client(std::make_shared<details::winrt_client>(std::move(config)))
468469
{
469470
}
470471

Release/tests/Functional/websockets/client/authentication_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ void auth_helper(test_websocket_server& server, utility::string_t username = U("
6464
TEST_FIXTURE(uri_address, auth_no_credentials, "Ignore:Linux", "NYI", "Ignore:Apple", "NYI")
6565
{
6666
test_websocket_server server;
67-
websocket_client client(m_uri);
67+
websocket_client client;
6868
auth_helper(server);
69-
VERIFY_THROWS(client.connect().wait(), websocket_exception);
69+
VERIFY_THROWS(client.connect(m_uri).wait(), websocket_exception);
7070
}
7171

7272
// Connect with credentials
@@ -76,18 +76,18 @@ TEST_FIXTURE(uri_address, auth_with_credentials, "Ignore:Linux", "NYI", "Ignore:
7676
websocket_client_config config;
7777
web::credentials cred(U("user"), U("password"));
7878
config.set_credentials(cred);
79-
websocket_client client(m_uri, config);
79+
websocket_client client(config);
8080

8181
auth_helper(server, cred.username(), cred.password());
82-
client.connect().wait();
82+
client.connect(m_uri).wait();
8383
client.close().wait();
8484
}
8585

8686
// Send and receive text message over SSL
8787
TEST_FIXTURE(uri_address, ssl_test, "Ignore", "Manual")
8888
{
89-
websocket_client client(U("wss://echo.websocket.org/"));
90-
client.connect().wait();
89+
websocket_client client;
90+
client.connect(U("wss://echo.websocket.org/")).wait();
9191
std::string body_str("hello");
9292

9393
auto receive_task = client.receive().then([body_str](websocket_incoming_message ret_msg)

Release/tests/Functional/websockets/client/client_construction.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ static void verify_client_invalid_argument(const uri &address)
5353
{
5454
try
5555
{
56-
websocket_client client(address);
56+
websocket_client client;
57+
client.connect(address).wait();
5758
VERIFY_IS_TRUE(false);
5859
} catch(std::invalid_argument &)
5960
{
@@ -80,22 +81,31 @@ TEST_FIXTURE(uri_address, get_client_config)
8081

8182
web::credentials cred(U("username"), U("password"));
8283
config.set_credentials(cred);
83-
websocket_client client(m_uri, config);
84+
websocket_client client(config);
8485

8586
const websocket_client_config& config2 = client.config();
8687
VERIFY_ARE_EQUAL(config2.credentials().username(), cred.username());
8788
VERIFY_ARE_EQUAL(config2.credentials().password(), cred.password());
8889
}
8990

90-
// Verify that we can get the baseuri from websocket_client constructors
91+
// Verify that we can get the baseuri from websocket_client connect.
9192
TEST_FIXTURE(uri_address, uri_test)
9293
{
93-
websocket_client client1(m_uri);
94+
websocket_client client1;
95+
VERIFY_ARE_EQUAL(client1.uri(), U("/"));
96+
97+
test_websocket_server server;
98+
client1.connect(m_uri).wait();
9499
VERIFY_ARE_EQUAL(client1.uri(), m_uri);
100+
client1.close().wait();
95101

96102
websocket_client_config config;
97-
websocket_client client2(m_uri, config);
103+
websocket_client client2(config);
104+
VERIFY_ARE_EQUAL(client2.uri(), U("/"));
105+
106+
client2.connect(m_uri).wait();
98107
VERIFY_ARE_EQUAL(client2.uri(), m_uri);
108+
client2.close().wait();
99109
}
100110

101111
TEST_FIXTURE(uri_address, move_operations)
@@ -104,9 +114,9 @@ TEST_FIXTURE(uri_address, move_operations)
104114
std::vector<unsigned char> body_vec(body.begin(), body.end());
105115

106116
test_websocket_server server;
107-
websocket_client client(m_uri);
117+
websocket_client client;
108118

109-
client.connect().wait();
119+
client.connect(m_uri).wait();
110120

111121
// Move constructor
112122
websocket_client client2 = std::move(client);
@@ -169,7 +179,7 @@ TEST_FIXTURE(uri_address, connect_with_headers)
169179
utility::string_t header_name(U("HeaderTest"));
170180
utility::string_t header_val(U("ConnectSuccessfully"));
171181
config.headers().add(header_name, header_val);
172-
websocket_client client(m_uri, config);
182+
websocket_client client(config);
173183

174184
server.set_http_handler([&](test_http_request request)
175185
{
@@ -180,7 +190,7 @@ TEST_FIXTURE(uri_address, connect_with_headers)
180190
resp.set_status_code(400); // Else fail the handshake, websocket client connect will fail in this case.
181191
return resp;
182192
});
183-
client.connect().wait();
193+
client.connect(m_uri).wait();
184194
client.close().wait();
185195
}
186196

Release/tests/Functional/websockets/client/close_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ TEST_FIXTURE(uri_address, close_client_websocket)
4444
{
4545
test_websocket_server server;
4646

47-
websocket_client client(m_uri);
47+
websocket_client client;
4848

49-
client.connect().wait();
49+
client.connect(m_uri).wait();
5050

5151
client.close().wait();
5252
}
@@ -56,9 +56,9 @@ TEST_FIXTURE(uri_address, close_with_reason)
5656
{
5757
test_websocket_server server;
5858

59-
websocket_client client(m_uri);
59+
websocket_client client;
6060

61-
client.connect().wait();
61+
client.connect(m_uri).wait();
6262

6363
client.close(websocket_close_status::going_away, U("Client disconnecting")).wait();
6464
}
@@ -69,9 +69,9 @@ TEST_FIXTURE(uri_address, close_from_server)
6969
std::string body("hello");
7070
test_websocket_server server;
7171

72-
websocket_client client(m_uri);
72+
websocket_client client;
7373

74-
client.connect().wait();
74+
client.connect(m_uri).wait();
7575

7676
// Send close frame from server
7777
test_websocket_msg msg;

0 commit comments

Comments
 (0)