-
Notifications
You must be signed in to change notification settings - Fork 4
Example WebSocket‐client
ANYKS edited this page Mar 19, 2024
·
8 revisions
#include <client/ws.hpp>
using namespace std;
using namespace awh;
using namespace awh::client;
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(const int32_t sid, const uint64_t rid, const client::web_t::agent_t agent, client::websocket_t * ws){
(void) sid;
(void) rid;
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 u_int 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) {}
};
int main(int argc, char * argv[]){
fmk_t fmk;
log_t log(&fmk);
client::core_t core(&fmk, &log);
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.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.callback <void (const awh::core_t::status_t)> ("status", std::bind(&Executor::status, &executor, _1));
ws.callback <void (const u_int, const string &)> ("errorWebsocket", std::bind(&Executor::error, &executor, _1, _2));
ws.callback <void (const vector <char> &, const bool)> ("messageWebsocket", std::bind(&Executor::message, &executor, _1, _2, &ws));
ws.callback <void (const int32_t, const uint64_t, const client::web_t::agent_t)> ("handshake", std::bind(&Executor::handshake, &executor, _1, _2, _3, &ws));
ws.start();
return 0;
}copyright © ANYKS