@@ -52,10 +52,10 @@ namespace pplx = Concurrency;
52
52
#include " cpprest/asyncrt_utils.h"
53
53
#include " cpprest/ws_msg.h"
54
54
55
- namespace web
55
+ namespace web
56
56
{
57
57
// / WebSocket client is currently in beta.
58
- namespace experimental
58
+ namespace experimental
59
59
{
60
60
namespace websockets
61
61
{
@@ -85,11 +85,56 @@ class websocket_client_config
85
85
public:
86
86
87
87
// / <summary>
88
- // / Creates a websocket client configuration with default settings.
88
+ // / Creates a websocket client configuration with default settings.
89
89
// / </summary>
90
- websocket_client_config ()
90
+ websocket_client_config () {}
91
+
92
+ // / <summary>
93
+ // / Copy constructor.
94
+ // / </summary>
95
+ websocket_client_config (const websocket_client_config &other) :
96
+ m_proxy (other.m_proxy),
97
+ m_credentials (other.m_credentials),
98
+ m_headers (other.m_headers)
91
99
{}
92
100
101
+ // / <summary>
102
+ // / Move constructor.
103
+ // / </summary>
104
+ websocket_client_config (websocket_client_config &&other) :
105
+ m_proxy (std::move(other.m_proxy)),
106
+ m_credentials (std::move(other.m_credentials)),
107
+ m_headers (std::move(other.m_headers))
108
+ {}
109
+
110
+ // / <summary>
111
+ // / Assignment operator.
112
+ // / </summary>
113
+ websocket_client_config &operator =(const websocket_client_config &other)
114
+ {
115
+ if (this != &other)
116
+ {
117
+ m_proxy = other.m_proxy ;
118
+ m_credentials = other.m_credentials ;
119
+ m_headers = other.m_headers ;
120
+ }
121
+ return *this ;
122
+ }
123
+
124
+ // / <summary>
125
+ // / Move assignment operator.
126
+ // / </summary>
127
+ websocket_client_config &operator =(websocket_client_config &&other)
128
+ {
129
+ if (this != &other)
130
+ {
131
+ m_proxy = std::move (other.m_proxy );
132
+ m_credentials = std::move (other.m_credentials );
133
+ m_headers = std::move (other.m_headers );
134
+ }
135
+ return *this ;
136
+ }
137
+
93
138
// / <summary>
94
139
// / Get the web proxy object
95
140
// / </summary>
@@ -103,9 +148,9 @@ class websocket_client_config
103
148
// / Set the web proxy object
104
149
// / </summary>
105
150
// / <param name="proxy">The web proxy object.</param>
106
- void set_proxy (web_proxy proxy)
151
+ void set_proxy (const web_proxy & proxy)
107
152
{
108
- m_proxy = std::move ( proxy) ;
153
+ m_proxy = proxy;
109
154
}
110
155
111
156
// / <summary>
@@ -121,9 +166,9 @@ class websocket_client_config
121
166
// / Set the client credentials
122
167
// / </summary>
123
168
// / <param name="cred">The client credentials.</param>
124
- void set_credentials (web::credentials cred)
169
+ void set_credentials (const web::credentials & cred)
125
170
{
126
- m_credentials = std::move ( cred) ;
171
+ m_credentials = cred;
127
172
}
128
173
129
174
// / <summary>
@@ -152,8 +197,8 @@ class websocket_client_config
152
197
// / Gets list of the specified subprotocols.
153
198
// / </summary>
154
199
// / <returns>Vector of all the subprotocols </returns>
155
- // / <remarks>If you want all the subprotocols in a comma separated string
156
- // / they can be directly directly looked up in the headers using 'Sec-WebSocket-Protocol'.</remarks>
200
+ // / <remarks>If you want all the subprotocols in a comma separated string
201
+ // / they can be directly looked up in the headers using 'Sec-WebSocket-Protocol'.</remarks>
157
202
_ASYNCRTIMP std::vector<::utility::string_t > subprotocols () const ;
158
203
159
204
private:
@@ -258,52 +303,40 @@ class websocket_exception : public std::exception
258
303
259
304
namespace details
260
305
{
261
- class winrt_client ;
262
- class ws_desktop_client ;
263
306
264
307
// Interface to be implemented by the websocket client implementations.
265
308
class _websocket_client_impl
266
309
{
267
310
public:
311
+
312
+ _websocket_client_impl (websocket_client_config config) :
313
+ m_config (std::move(config)) {}
314
+
268
315
virtual ~_websocket_client_impl () _noexcept {}
269
316
270
317
virtual pplx::task<void > connect () = 0;
271
318
272
- virtual pplx::task<void > send (websocket_outgoing_message msg) = 0;
319
+ virtual pplx::task<void > send (websocket_outgoing_message & msg) = 0;
273
320
274
- virtual pplx::task<websocket_incoming_message> receive () = 0;
321
+ virtual pplx::task<websocket_incoming_message> receive () = 0;
275
322
276
323
virtual pplx::task<void > close () = 0;
277
324
278
325
virtual pplx::task<void > close (websocket_close_status close_status, const utility::string_t &close_reason=_XPLATSTR(" " )) = 0;
279
326
280
- // / <summary>
281
- // / Gets the base uri
282
- // / </summary>
283
- // / <returns>
284
- // / A base uri initialized in constructor
285
- // / </return>
286
327
const web::uri& uri () const
287
328
{
288
329
return m_uri;
289
330
}
290
331
291
- // / <summary>
292
- // / Set the base uri.
293
- // / </summary>
294
- // / <param name="uri"> The user specified uri. </param>
295
- void set_uri (web::uri uri)
332
+ void set_uri (const web::uri &uri)
296
333
{
297
- m_uri = std::move ( uri) ;
334
+ m_uri = uri;
298
335
}
299
336
300
- // / <summary>
301
- // / Get client configuration object
302
- // / </summary>
303
- // / <returns>A reference to the client configuration object.</returns>
304
337
const websocket_client_config& config () const
305
338
{
306
- return m_client_config ;
339
+ return m_config ;
307
340
}
308
341
309
342
static void verify_uri (const web::uri& uri)
@@ -328,16 +361,9 @@ class _websocket_client_impl
328
361
}
329
362
}
330
363
331
-
332
364
protected:
333
365
web::uri m_uri;
334
-
335
- _websocket_client_impl (websocket_client_config client_config)
336
- : m_client_config(std::move(client_config))
337
- {
338
- }
339
-
340
- websocket_client_config m_client_config;
366
+ websocket_client_config m_config;
341
367
};
342
368
}
343
369
@@ -350,48 +376,58 @@ class websocket_client
350
376
// / <summary>
351
377
// / Creates a new websocket_client.
352
378
// / </summary>
353
- _ASYNCRTIMP websocket_client ();
379
+ websocket_client () : websocket_client(websocket_client_config()) {}
354
380
355
381
// / <summary>
356
382
// / Creates a new websocket_client.
357
383
// / </summary>
358
- // / <param name="client_config">The client configuration object containing the possible configuration options to intitialize the <c>websocket_client</c>. </param>
384
+ // / <param name="client_config">The client configuration object containing the possible configuration options to initialize the <c>websocket_client</c>. </param>
359
385
_ASYNCRTIMP websocket_client (websocket_client_config client_config);
360
386
361
387
// / <summary>
362
388
// / Destructor
363
389
// / </summary>
364
- ~websocket_client () { }
390
+ ~websocket_client () _noexcept { }
365
391
366
392
// / <summary>
367
- // / Connects to the remote network destination. The connect method initiates the websocket handshake with the
393
+ // / Connects to the remote network destination. The connect method initiates the websocket handshake with the
368
394
// / remote network destination, takes care of the protocol upgrade request.
369
395
// / </summary>
370
396
// / <param name="uri">The uri address to connect. </param>
371
397
// / <returns>An asynchronous operation that is completed once the client has successfully connected to the websocket server.</returns>
372
- pplx::task<void > connect (web::uri uri)
373
- {
374
- m_client->set_uri (std::move (uri));
375
- return m_client->connect ();
398
+ pplx::task<void > connect (const web::uri &uri)
399
+ {
400
+ m_client->verify_uri (uri);
401
+ m_client->set_uri (uri);
402
+ return m_client->connect ();
376
403
}
377
404
378
405
// / <summary>
379
406
// / Sends a websocket message to the server .
380
407
// / </summary>
381
408
// / <returns>An asynchronous operation that is completed once the message is sent.</returns>
382
- pplx::task<void > send (websocket_outgoing_message msg) { return m_client->send (msg); }
409
+ pplx::task<void > send (websocket_outgoing_message msg)
410
+ {
411
+ return m_client->send (msg);
412
+ }
383
413
384
414
// / <summary>
385
415
// / Receive a websocket message.
386
416
// / </summary>
387
417
// / <returns>An asynchronous operation that is completed when a message has been received by the client endpoint.</returns>
388
- pplx::task<websocket_incoming_message> receive () { return m_client->receive (); }
418
+ pplx::task<websocket_incoming_message> receive ()
419
+ {
420
+ return m_client->receive ();
421
+ }
389
422
390
423
// / <summary>
391
424
// / Closes a websocket client connection, sends a close frame to the server and waits for a close message from the server.
392
425
// / </summary>
393
426
// / <returns>An asynchronous operation that is completed the connection has been successfully closed.</returns>
394
- pplx::task<void > close () { return m_client->close (); }
427
+ pplx::task<void > close ()
428
+ {
429
+ return m_client->close ();
430
+ }
395
431
396
432
// / <summary>
397
433
// / Closes a websocket client connection, sends a close frame to the server and waits for a close message from the server.
@@ -400,16 +436,14 @@ class websocket_client
400
436
// / <param name="close_reason">While closing an established connection, an endpoint may indicate the reason for closure.</param>
401
437
// / <returns>An asynchronous operation that is completed the connection has been successfully closed.</returns>
402
438
pplx::task<void > close (websocket_close_status close_status, const utility::string_t & close_reason=_XPLATSTR(" " ))
403
- {
404
- return m_client->close (close_status, close_reason);
439
+ {
440
+ return m_client->close (close_status, close_reason);
405
441
}
406
442
407
443
// / <summary>
408
444
// / Gets the websocket client URI.
409
445
// / </summary>
410
- // / <returns>
411
- // / A base uri initialized in constructor
412
- // / </return>
446
+ // / <returns>URI connected to.</returns>
413
447
const web::uri& uri () const
414
448
{
415
449
return m_client->uri ();
@@ -425,7 +459,6 @@ class websocket_client
425
459
}
426
460
427
461
private:
428
-
429
462
std::shared_ptr<details::_websocket_client_impl> m_client;
430
463
};
431
464
0 commit comments