VC - vs2017里poco的websocket使用netssl

WebRTC进阶-信令篇-之十六 :基于poco-c++-libraries开发实现wss的Windows客户端

WebRTC进阶-信令篇-之十六 :基于poco-c++-libraries开发实现wss的Windows客户端 - 烈火残躯 - 博客园

websocket实现同步c++_sun007700的专栏-CSDN博客_websocket 帧同步

https://github.com/pocoproject/poco/blob/master/Net/samples/WebSocketServer/src/WebSocketServer.cpp

1. 首先下载openssl库

Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions

2. Install OpenSSL (we assume you install to d:\OpenSSL32)
3. 编辑buildwin.cmd,添加如下内容:

set OPENSSL_DIR=d:\OpenSSL32
set OPENSSL_INCLUDE=%OPENSSL_DIR%\include
set OPENSSL_LIB=%OPENSSL_DIR%\lib;%OPENSSL_DIR%\lib\VC
set INCLUDE=%INCLUDE%;%OPENSSL_INCLUDE%
set LIB=%LIB%;%OPENSSL_LIB%

然后使用git bash命令

mkdir cmake_build_x86
cd cmake_build_x86
cmake -G "Visual Studio 15 2017" ..


 

打开工程,看到netssl模块,开始编译即可

其他工程使用的时候,在工程里添加

d:\OpenSSL32\include

d:\OpenSSL32\lib\VC

代码例子

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;

}

使用Poco C++库创建websocket安全访问(wss)客户端_易贤网

websocket c++ example - Bigben - 博客园

/============================================================================
 
#include <iostream>
 
#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>
 
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 namespace std;
 
int ws_main() {
    char buffer[1024];
    int flags;
    int n;
    std::string payload;
 
    try {
        HTTPClientSession cs("echo.websocket.org", 80);
        HTTPRequest request(HTTPRequest::HTTP_GET, "/", "HTTP/1.1");
        HTTPResponse response;
        std::string cmd;
 
        WebSocket * ws = new WebSocket(cs, request, response); // Causes the timeout
 
        payload = "SGClient: Hello World!";
        cout << "Send: SGClient: Hello World!" << endl;
        ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT);
        n = ws->receiveFrame(buffer, sizeof(buffer), flags);
        buffer[n] = '\0';
        cout << "Received: " << buffer << endl;
 
        while (cmd != "exit") {
            cmd = "";
            cout << "Please input[exit]:";
            std::cin >> cmd;
            ws->sendFrame(cmd.data(), cmd.size(), WebSocket::FRAME_TEXT);
            n = ws->receiveFrame(buffer, sizeof(buffer), flags);
            buffer[n] = '\0';
            if (n > 0) {
                std::cout << "Receive: " << buffer << std::endl;
            }
        }
 
        ws->shutdown();
    } catch (Exception ex) {
        cout << ex.displayText() << endl;
        cout << ex.what() << endl;
        return -1;
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值