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; }