mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
git-subtree-dir: src/websocketpp git-subtree-split: 875d420952d2c47d38de15952a401f00773cdffc
53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#include <iostream>
|
|
|
|
#include <websocketpp/config/asio_no_tls.hpp>
|
|
#include <websocketpp/server.hpp>
|
|
|
|
typedef websocketpp::server<websocketpp::config::asio> server;
|
|
|
|
using websocketpp::connection_hdl;
|
|
using websocketpp::lib::placeholders::_1;
|
|
using websocketpp::lib::placeholders::_2;
|
|
using websocketpp::lib::bind;
|
|
using websocketpp::lib::ref;
|
|
|
|
|
|
bool validate(server & s, connection_hdl hdl) {
|
|
server::connection_ptr con = s.get_con_from_hdl(hdl);
|
|
|
|
std::cout << "Cache-Control: " << con->get_request_header("Cache-Control") << std::endl;
|
|
|
|
const std::vector<std::string> & subp_requests = con->get_requested_subprotocols();
|
|
std::vector<std::string>::const_iterator it;
|
|
|
|
for (it = subp_requests.begin(); it != subp_requests.end(); ++it) {
|
|
std::cout << "Requested: " << *it << std::endl;
|
|
}
|
|
|
|
if (subp_requests.size() > 0) {
|
|
con->select_subprotocol(subp_requests[0]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
server s;
|
|
|
|
s.set_validate_handler(bind(&validate,ref(s),::_1));
|
|
|
|
s.init_asio();
|
|
s.listen(9005);
|
|
s.start_accept();
|
|
|
|
s.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;
|
|
}
|
|
}
|