Updating example to close when they receive "world".

This commit is contained in:
David Baird
2013-05-14 11:11:39 -06:00
parent 50c2d0e18d
commit b7825802b9
2 changed files with 10 additions and 9 deletions
+4 -3
View File
@@ -12,11 +12,12 @@ int main()
WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo"); WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo");
assert(ws); assert(ws);
ws->send("goodbye"); ws->send("goodbye");
while (true) { ws->send("hello");
while (ws->getReadyState() != WebSocket::CLOSED) {
ws->poll(); ws->poll();
ws->dispatch([](const std::string & message) { ws->dispatch([ws](const std::string & message) {
printf(">>> %s\n", message.c_str()); printf(">>> %s\n", message.c_str());
ws->close(); if (message == "world") { ws->close(); }
}); });
} }
return 0; return 0;
+6 -6
View File
@@ -4,24 +4,24 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
using easywsclient::WebSocket;
static WebSocket::pointer ws = NULL;
void handle_message(const std::string & message) void handle_message(const std::string & message)
{ {
printf(">>> %s\n", message.c_str()); printf(">>> %s\n", message.c_str());
if (message == "world") { ws->close(); }
} }
int main() int main()
{ {
using easywsclient::WebSocket; ws = WebSocket::from_url("ws://localhost:8126/foo");
WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo");
assert(ws); assert(ws);
ws->send("goodbye"); ws->send("goodbye");
ws->send("hello"); ws->send("hello");
ws->close(); while (ws->getReadyState() != WebSocket::CLOSED) {
while(ws->getReadyState() != WebSocket::CLOSED)
{
ws->poll(); ws->poll();
ws->dispatch(handle_message); ws->dispatch(handle_message);
} }
return 0; return 0;
} }