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;





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 ("", 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.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 0;



WS client creation is no longer redundant, and there are examples from the authorities.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.