Poco WebSocket Library Features:
1, create WebSocket client using Http/https clientsession
2, is synchronized, this should be sufficient for C + + desktop programming.
3, rely on OpenSSL.
The code is as follows |
|
#include "stdafx.h" #include <iostream> #include <assert.h> #include "Poco/net/websocket.h" #include "Poco/net/httpclientsession.h" #include "Poco/net/httprequest.h" #include "poco/net/httpresponse.h" #include "Poco/net/serversocket.h" #include "Poco/net/netexception.h" #include "Poco/exception.h" #include "Poco/net/httpsclientsession.h" #include "Poco/uristreamopener.h" #include "Poco/streamcopier.h" #include "poco/path.h" #include "poco/uri.h" #include "Poco/sharedptr.h" #include "Poco/exception.h" #include "Poco/net/httpstreamfactory.h" #include "Poco/net/httpsstreamfactory.h" #include "Poco/net/ftpstreamfactory.h" #include "Poco/net/sslmanager.h" #include "Poco/net/keyconsolehandler.h" #include "Poco/net/consolecertificatehandler.h" #include "Poco/net/acceptcertificatehandler.h" #include "Poco/net/securestreamsocket.h" #include "poco/net/x509certificate.h" #include <memory> Using Poco::net::httpsclientsession; Using Poco::net::httpclientsession; Using Poco::net::httprequest; Using Poco::net::httpresponse; Using Poco::net::httpserverrequest; Using Poco::net::httpserverresponse; Using Poco::net::websocket; Using Poco::net::websocketexception; Using Poco::exception; Using Poco::uristreamopener; Using Poco::streamcopier; Using Poco::P ath; Using Poco::uri; Using Poco::sharedptr; Using Poco::exception; Using Poco::net::httpstreamfactory; Using Poco::net::httpsstreamfactory; Using Poco::net::ftpstreamfactory; Using Poco::net::sslmanager; Using Poco::net::context; Using Poco::net::keyconsolehandler; Using poco::net::P rivatekeypassphrasehandler; Using Poco::net::invalidcertificatehandler; Using Poco::net::consolecertificatehandler; Using Poco::net::acceptcertificatehandler; Using Poco::net::x509certificate; int main (int argc, _tchar* argv[]) { Char buffer[1024]; int flags; int n; std::string payload; Try { Poco::net::initializessl (); sharedptr<invalidcertificatehandler> Pcert = new Consolecertificatehandler (false); Ask the user via console Context::P TR PContext = new context (Context::tlsv1_client_use, "", "", "Rootcert.pem", context::verify_relaxed, 9, False , "all:! Adh:! low:! Exp:! MD5: @STRENGTH "); Sslmanager::instance (). Initializeclient (0, Pcert, pContext); Httpsclientsession CS ("echo.websocket.org", 443); HttpRequest request (Httprequest::http_get, "/", "http/1.1"); HttpResponse response; std::string cmd; WebSocket * ws = new WebSocket (CS, request, response); Causes the timeout Payload = "Shakethand"; Ws->sendframe (Payload.data (), Payload.size (), websocket::frame_text); n = ws->receiveframe (buffer, sizeof (buffer), flags); std::cout<< "return:" <<std::string (buffer,n) << "\ n"; Ws->shutdown (); } catch (poco::exception& ex) { Std::cout << "Error: <<__FILE__<<": "<<__LINE__<<": "<< Ex.displaytext () < < "\ n"; return-1; } return 0; } |
WS client creation is no longer redundant, and there are examples from the authorities.