Files
easywsclient/example-client-cpp11.cpp
T
2013-09-27 14:14:43 -06:00

28 lines
911 B
C++

// Compile with:
// g++ -std=gnu++0x example-client-cpp11.cpp -o example-client-cpp11
#include "easywsclient.hpp"
//#include "easywsclient.cpp" // <-- include only if you don't want compile separately
#include <assert.h>
#include <stdio.h>
#include <string>
#include <memory>
int main()
{
using easywsclient::WebSocket;
std::unique_ptr<WebSocket> ws(WebSocket::from_url("ws://localhost:8126/foo"));
assert(ws);
ws->send("goodbye");
ws->send("hello");
while (ws->getReadyState() != WebSocket::CLOSED) {
WebSocket::pointer wsp = &*ws; // <-- because a unique_ptr cannot be copied into a lambda
ws->poll();
ws->dispatch([wsp](const std::string & message) {
printf(">>> %s\n", message.c_str());
if (message == "world") { wsp->close(); }
});
}
// N.B. - unique_ptr will free the WebSocket instance upon return:
return 0;
}