From b7825802b97dfb67f7a1d0c7d3911108243cbe19 Mon Sep 17 00:00:00 2001 From: David Baird Date: Tue, 14 May 2013 11:11:39 -0600 Subject: [PATCH] Updating example to close when they receive "world". --- example-client-cpp11.cpp | 7 ++++--- example-client.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/example-client-cpp11.cpp b/example-client-cpp11.cpp index 93b2cc9..9926126 100644 --- a/example-client-cpp11.cpp +++ b/example-client-cpp11.cpp @@ -12,11 +12,12 @@ int main() WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); assert(ws); ws->send("goodbye"); - while (true) { + ws->send("hello"); + while (ws->getReadyState() != WebSocket::CLOSED) { ws->poll(); - ws->dispatch([](const std::string & message) { + ws->dispatch([ws](const std::string & message) { printf(">>> %s\n", message.c_str()); - ws->close(); + if (message == "world") { ws->close(); } }); } return 0; diff --git a/example-client.cpp b/example-client.cpp index 5de4d71..0016fc7 100644 --- a/example-client.cpp +++ b/example-client.cpp @@ -4,24 +4,24 @@ #include #include +using easywsclient::WebSocket; +static WebSocket::pointer ws = NULL; + void handle_message(const std::string & message) { printf(">>> %s\n", message.c_str()); + if (message == "world") { ws->close(); } } int main() { - using easywsclient::WebSocket; - WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); + ws = WebSocket::from_url("ws://localhost:8126/foo"); assert(ws); ws->send("goodbye"); ws->send("hello"); - ws->close(); - while(ws->getReadyState() != WebSocket::CLOSED) - { + while (ws->getReadyState() != WebSocket::CLOSED) { ws->poll(); ws->dispatch(handle_message); } - return 0; }