Create a WebSocket secure access (WSS) client using the Poco C + + library

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.