使用Poco C++庫建立websocket安全訪問(wss)用戶端

來源:互聯網
上載者:User

   Poco websocket庫特點:

  1,使用http/https ClientSession建立websocket client

  2,是同步的,這對C++案頭編程來說應該是夠用的.

  3,依賴openssl.

 代碼如下  
#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::Path;
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::PrivateKeyPassphraseHandler;
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::Ptr 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用戶端建立不再贅述,官方已有範例.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.