-
Couldn't load subscription status.
- Fork 4
Example WebSocket‐client
ANYKS edited this page Sep 1, 2025
·
8 revisions
#include <awh/client/ws.hpp>
using namespace awh;
using namespace placeholders;
class Executor {
private:
const fmk_t * _fmk;
const log_t * _log;
public:
void status(const awh::core_t::status_t status){
switch(static_cast <uint8_t> (status)){
case static_cast <uint8_t> (awh::core_t::status_t::START):
this->_log->print("START", log_t::flag_t::INFO);
break;
case static_cast <uint8_t> (awh::core_t::status_t::STOP):
this->_log->print("STOP", log_t::flag_t::INFO);
break;
}
}
void handshake([[maybe_unused]] const int32_t sid, [[maybe_unused]] const uint64_t rid, const client::web_t::agent_t agent, client::websocket_t * ws){
if(agent == client::web_t::agent_t::WEBSOCKET){
this->_log->print("Handshake", log_t::flag_t::INFO);
const string query = "Hello World!!!";
ws->sendMessage(vector <char> (query.begin(), query.end()));
}
}
public:
void error(const uint32_t code, const string & mess){
this->_log->print("%s [%u]", log_t::flag_t::CRITICAL, mess.c_str(), code);
}
void message(const vector <char> & buffer, const bool utf8, client::websocket_t * ws){
string subprotocol = "";
const auto subprotocols = ws->subprotocols();
if(!subprotocols.empty())
subprotocol = (* subprotocols.begin());
if(utf8){
cout << "MESSAGE: " << string(buffer.begin(), buffer.end()) << endl;
cout << "SUB PROTOCOL: " << subprotocol << endl;
}
}
public:
Executor(const fmk_t * fmk, const log_t * log) : _fmk(fmk), _log(log) {}
};
int32_t main(int32_t argc, char * argv[]){
fmk_t fmk;
log_t log(&fmk);
client::core_t core(&fmk, &log);
client::websocket_t ws(&core, &fmk, &log);
Executor executor(&fmk, &log);
log.name("WebSocket Client");
log.format("%H:%M:%S %d.%m.%Y");
ws.mode({
client::web_t::flag_t::ALIVE,
client::web_t::flag_t::REDIRECTS,
client::web_t::flag_t::TAKEOVER_CLIENT,
client::web_t::flag_t::TAKEOVER_SERVER,
client::web_t::flag_t::CONNECT_METHOD_ENABLE
});
core.sonet(awh::scheme_t::sonet_t::TLS);
core.proto(awh::engine_t::proto_t::HTTP2);
// core.proto(awh::engine_t::proto_t::HTTP1_1);
node_t::ssl_t ssl;
ssl.verify = false;
ssl.key = "./certs/certificates/client-key.pem";
ssl.cert = "./certs/certificates/client-cert.pem";
core.ssl(ssl);
// ws.proxy("http://user:[email protected]:port");
// ws.proxy("https://user:[email protected]:port");
// ws.proxy("socks5://user:[email protected]:port");
// ws.proxy(client::scheme_t::work_t::ALLOW);
// ws.proxy(client::scheme_t::work_t::DISALLOW);
// ws.authTypeProxy(auth_t::type_t::BASIC);
// ws.authTypeProxy(auth_t::type_t::DIGEST, auth_t::hash_t::MD5);
ws.user("user", "password");
// ws.authType(awh::auth_t::type_t::BASIC);
ws.authType(awh::auth_t::type_t::DIGEST, awh::auth_t::hash_t::MD5);
ws.init("wss://127.0.0.1:2222", {awh::http_t::compressor_t::DEFLATE});
ws.subprotocols({"test2", "test8", "test9"});
// ws.extensions({{"test1", "test2", "test3"},{"good1", "good2", "good3"}});
ws.on <void (const awh::core_t::status_t)> ("status", &Executor::status, &executor, _1);
ws.on <void (const uint32_t, const string &)> ("errorWebsocket", &Executor::error, &executor, _1, _2);
ws.on <void (const vector <char> &, const bool)> ("messageWebsocket", &Executor::message, &executor, _1, _2, &ws);
ws.on <void (const int32_t, const uint64_t, const client::web_t::agent_t)> ("handshake", &Executor::handshake, &executor, _1, _2, _3, &ws);
ws.start();
return EXIT_SUCCESS;
}copyright © ANYKS