Created
June 12, 2015 01:58
-
-
Save redboltz/f0459a12edb199a38583 to your computer and use it in GitHub Desktop.
Websocket++ event handling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -std=c++11 -Wall -Wextra client.cpp -lboost_system -pthread | |
#define _WEBSOCKETPP_CPP11_STL_ | |
#define _WEBSOCKETPP_RVALUE_REFERENCES_ | |
#define _WEBSOCKETPP_DELETED_FUNCTIONS_ | |
#include <iostream> | |
#include <websocketpp/config/asio_no_tls.hpp> | |
#include <websocketpp/client.hpp> | |
typedef websocketpp::client<websocketpp::config::asio> client; | |
int main(int argc, char** argv) { | |
if (argc != 2) { | |
std::cout << "Usage: " << argv[0] << " url" << std::endl; | |
return -1; | |
} | |
client c; | |
boost::asio::io_service ios; | |
c.init_asio(&ios); | |
try { | |
c.set_open_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "open: " << hdl.lock() << std::endl; | |
}); | |
c.set_close_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "close: " << hdl.lock() << std::endl; | |
}); | |
c.set_fail_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "fail: " << hdl.lock() << std::endl; | |
}); | |
c.set_ping_handler( | |
[](websocketpp::connection_hdl hdl, std::string str) -> bool { | |
std::cout << "ping: " << hdl.lock() << std::endl; | |
std::cout << str << std::endl; | |
return true; | |
}); | |
c.set_pong_handler( | |
[](websocketpp::connection_hdl hdl, std::string str) -> bool { | |
std::cout << "pong: " << hdl.lock() << std::endl; | |
std::cout << str << std::endl; | |
return true; | |
}); | |
c.set_interrupt_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "interrupt: " << hdl.lock() << std::endl; | |
}); | |
c.set_http_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "http: " << hdl.lock() << std::endl; | |
}); | |
c.set_validate_handler( | |
[](websocketpp::connection_hdl hdl) -> bool { | |
std::cout << "validate: " << hdl.lock() << std::endl; | |
return true; | |
}); | |
c.set_message_handler( | |
[&](websocketpp::connection_hdl hdl, | |
client::message_ptr msg){ | |
std::cout << "message: " << hdl.lock() << std::endl; | |
std::cout << msg->get_payload() << std::endl; | |
}); | |
std::string uri = argv[1]; | |
websocketpp::lib::error_code ec; | |
client::connection_ptr con = c.get_connection(uri, ec); | |
std::cout << ec.message() << std::endl; | |
c.connect(con); | |
c.run(); | |
} catch (const std::exception & e) { | |
std::cout << e.what() << std::endl; | |
} catch (websocketpp::lib::error_code e) { | |
std::cout << e.message() << std::endl; | |
} catch (...) { | |
std::cout << "other exception" << std::endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -std=c++11 -Wall -Wextra server.cpp -lboost_system | |
#define _WEBSOCKETPP_CPP11_STL_ | |
#define _WEBSOCKETPP_RVALUE_REFERENCES_ | |
#define _WEBSOCKETPP_DELETED_FUNCTIONS_ | |
#include <iostream> | |
#include <websocketpp/config/asio_no_tls.hpp> | |
#include <websocketpp/server.hpp> | |
#include <boost/lexical_cast.hpp> | |
typedef websocketpp::server<websocketpp::config::asio> server; | |
int main(int argc, char** argv) { | |
if (argc != 2) { | |
std::cout << "Usage: " << argv[0] << " port" << std::endl; | |
return -1; | |
} | |
std::uint16_t port = boost::lexical_cast<std::uint16_t>(argv[1]); | |
server s; | |
boost::asio::io_service ios; | |
s.set_reuse_addr(true); | |
s.set_open_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "open: " << hdl.lock() << std::endl; | |
}); | |
s.set_close_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "close: " << hdl.lock() << std::endl; | |
}); | |
s.set_fail_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "fail: " << hdl.lock() << std::endl; | |
}); | |
s.set_ping_handler( | |
[](websocketpp::connection_hdl hdl, std::string str) -> bool { | |
std::cout << "ping: " << hdl.lock() << std::endl; | |
std::cout << str << std::endl; | |
return true; | |
}); | |
s.set_pong_handler( | |
[](websocketpp::connection_hdl hdl, std::string str) -> bool { | |
std::cout << "pong: " << hdl.lock() << std::endl; | |
std::cout << str << std::endl; | |
return true; | |
}); | |
s.set_interrupt_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "interrupt: " << hdl.lock() << std::endl; | |
}); | |
s.set_http_handler( | |
[](websocketpp::connection_hdl hdl){ | |
std::cout << "http: " << hdl.lock() << std::endl; | |
}); | |
s.set_validate_handler( | |
[](websocketpp::connection_hdl hdl) -> bool { | |
std::cout << "validate: " << hdl.lock() << std::endl; | |
return true; | |
}); | |
s.set_message_handler( | |
[&](websocketpp::connection_hdl hdl, | |
server::message_ptr msg){ | |
std::cout << "message: " << hdl.lock() << std::endl; | |
std::cout << msg->get_payload() << std::endl; | |
}); | |
s.init_asio(&ios); | |
s.listen(port); | |
s.start_accept(); | |
ios.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment