Skip to content

Commit 842f745

Browse files
author
Peter Thorson
committed
Add echo_client example
1 parent 17a2647 commit 842f745

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

SConstruct

+3
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ if not env['PLATFORM'].startswith('win'):
231231
# echo_server
232232
echo_server = SConscript('#/examples/echo_server/SConscript',variant_dir = builddir + 'echo_server',duplicate = 0)
233233

234+
# echo_client
235+
echo_client = SConscript('#/examples/echo_client/SConscript',variant_dir = builddir + 'echo_client',duplicate = 0)
236+
234237
# echo_server_tls
235238
if tls_build:
236239
echo_server_tls = SConscript('#/examples/echo_server_tls/SConscript',variant_dir = builddir + 'echo_server_tls',duplicate = 0)

changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ HEAD
2727
and `string::empty()`. This avoids generating unnecessary temporary objects.
2828
#468 Thank you Vladislav Yaroslavlev for reporting and a patch.
2929
- Documentation: Adds an example demonstrating the use of external `io_service`
30+
- Documentation: Adds a simple echo_client example.
31+
- Documentation: Begins migration of the web based user manual into Doxygen.
3032
- Bug: Fix memory leak when init_asio produces an error. #454 Thank you Mark
3133
Grimes for reporting and fixing.
3234
- Bug: Fix crash when processing a specially crafted HTTP header. Thank you Eli

examples/echo_client/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
file (GLOB SOURCE_FILES *.cpp)
3+
file (GLOB HEADER_FILES *.hpp)
4+
5+
init_target (echo_client)
6+
7+
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
8+
9+
link_boost ()
10+
final_target ()
11+
12+
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")

examples/echo_client/SConscript

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## echo_client example
2+
##
3+
4+
Import('env')
5+
Import('env_cpp11')
6+
Import('boostlibs')
7+
Import('platform_libs')
8+
Import('polyfill_libs')
9+
10+
env = env.Clone ()
11+
env_cpp11 = env_cpp11.Clone ()
12+
13+
prgs = []
14+
15+
# if a C++11 environment is available build using that, otherwise use boost
16+
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
17+
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + ['z']
18+
prgs += env_cpp11.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS)
19+
else:
20+
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs] + ['z']
21+
prgs += env.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS)
22+
23+
Return('prgs')

examples/echo_client/echo_client.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2016, Peter Thorson. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* * Redistributions of source code must retain the above copyright
7+
* notice, this list of conditions and the following disclaimer.
8+
* * Redistributions in binary form must reproduce the above copyright
9+
* notice, this list of conditions and the following disclaimer in the
10+
* documentation and/or other materials provided with the distribution.
11+
* * Neither the name of the WebSocket++ Project nor the
12+
* names of its contributors may be used to endorse or promote products
13+
* derived from this software without specific prior written permission.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*
26+
*/
27+
28+
#include <websocketpp/config/asio_no_tls_client.hpp>
29+
#include <websocketpp/client.hpp>
30+
31+
#include <iostream>
32+
33+
typedef websocketpp::client<websocketpp::config::asio_client> client;
34+
35+
using websocketpp::lib::placeholders::_1;
36+
using websocketpp::lib::placeholders::_2;
37+
using websocketpp::lib::bind;
38+
39+
// pull out the type of messages sent by our config
40+
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
41+
42+
// This message handler will be invoked once for each incoming message. It
43+
// prints the message and then sends a copy of the message back to the server.
44+
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
45+
std::cout << "on_message called with hdl: " << hdl.lock().get()
46+
<< " and message: " << msg->get_payload()
47+
<< std::endl;
48+
49+
50+
websocketpp::lib::error_code ec;
51+
52+
c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
53+
if (ec) {
54+
std::cout << "Echo failed because: " << ec.message() << std::endl;
55+
}
56+
}
57+
58+
int main(int argc, char* argv[]) {
59+
// Create a client endpoint
60+
client c;
61+
62+
std::string uri = "ws://localhost:9002";
63+
64+
if (argc == 2) {
65+
uri = argv[1];
66+
}
67+
68+
try {
69+
// Set logging to be pretty verbose (everything except message payloads)
70+
c.set_access_channels(websocketpp::log::alevel::all);
71+
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
72+
73+
// Initialize ASIO
74+
c.init_asio();
75+
76+
// Register our message handler
77+
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
78+
79+
websocketpp::lib::error_code ec;
80+
client::connection_ptr con = c.get_connection(uri, ec);
81+
if (ec) {
82+
std::cout << "could not create connection because: " << ec.message() << std::endl;
83+
return 0;
84+
}
85+
86+
// Note that connect here only requests a connection. No network messages are
87+
// exchanged until the event loop starts running in the next line.
88+
c.connect(con);
89+
90+
// Start the ASIO io_service run loop
91+
// this will cause a single connection to be made to the server. c.run()
92+
// will exit when this connection is closed.
93+
c.run();
94+
} catch (websocketpp::exception const & e) {
95+
std::cout << e.what() << std::endl;
96+
}
97+
}

0 commit comments

Comments
 (0)