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.