C ++ SSL WebSocket连接到CoinBase [英] C++ SSL WebSocket Connection to CoinBase

查看:131
本文介绍了C ++ SSL WebSocket连接到CoinBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Poco图书馆的新手,可以帮助我理解错误的位置。我一直在网上搜索SSL Websocket客户端的好例子,并且似乎没有。



这是我目前正在尝试调试的代码。

它应该打开一个websocket到服务器并订阅服务。



更新:

感谢使用Poco在c ++中进行HTTPS请求,我现在已经过了WebSocket初始化,但我没有看到服务器对订阅消息的回复。





I'm new to the Poco Libraries and could use a little help in understanding where my error is. I've been scouring the web for good examples SSL Websocket clients and there doesn't appear to be any.

Here is the code I am currently attempting to debug.
It should open a websocket to the server and subscribe to the service.

Update:
Thanks to HTTPS request in c++ using Poco, I am now getting past the WebSocket Initialization, but I am not seeing a response from the server for the subscribe message.


bool CMAIMarketFeedCoinBaseEngine::ConnectToServer(void)
{
	Poco::Net::initializeSSL();

	Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrHandler = new Poco::Net::AcceptCertificateHandler(false);
    Poco::Net::Context context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
	Poco::Net::SSLManager::instance().initializeClient(0, ptrHandler, &context);

	Poco::Net::HTTPSClientSession Client("ws-feed.exchange.coinbase.com", 443, &context);

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/wss", Poco::Net::HTTPMessage::HTTP_1_1);
    Poco::Net::HTTPResponse response;

    try 
	{
		Poco::Net::WebSocket webSocket(Client, request, response);

		Poco::JSON::Object JSON_Subscribe;
		JSON_Subscribe.set("type",		"suscribe");
		JSON_Subscribe.set("product_id", "BTC-USD");

		std::ostringstream stream;
		JSON_Subscribe.stringify(stream);
		std::string str =  stream.str();

		char receiveBuff[256];

		int len = webSocket.sendFrame(str.data(), str.size(), Poco::Net::WebSocket::FRAME_TEXT);
		std::cout << "Sent bytes " << len << std::endl;
		int flags=0;

		int rlen = webSocket.receiveFrame(receiveBuff,256,flags);
		std::cout << "Received bytes " << rlen << std::endl;
		std::cout << receiveBuff << std::endl;

		webSocket.close();
    } 
	catch (std::exception &e) 
	{
        std::cout << "Exception " << e.what();
    }

	return true;
}





实例化websocket时出现异常。

__vfptr 0x5c7080e8 {PocoNetSSLd .dll!const Poco :: Net :: SSLException ::`vftable'} {0x5c6b1622 {PocoNetSSLd.dll!Poco :: Net :: SSLException ::`vector deletion destructor'(unsigned int)},...} void * *







I get an exception when the websocket is instantiated.
__vfptr 0x5c7080e8 {PocoNetSSLd.dll!const Poco::Net::SSLException::`vftable'} {0x5c6b1622 {PocoNetSSLd.dll!Poco::Net::SSLException::`vector deleting destructor'(unsigned int)}, ...} void * *


bool ConnectToServer(void)
{
	Poco::Net::initializeSSL();

	Poco::Net::Context context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

	Poco::Net::HTTPSClientSession Client("ws-feed.exchange.coinbase.com", 443, &context);

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/", Poco::Net::HTTPMessage::HTTP_1_1);
    Poco::Net::HTTPResponse response;

    try 
	{
		Poco::Net::WebSocket webSocket(Client, request, response);

		Poco::JSON::Object JSON_Subscribe;
		JSON_Subscribe.set("type",		"suscribe");
		JSON_Subscribe.set("product_id", "BTC-USD");

		std::ostringstream stream;
		JSON_Subscribe.stringify(stream);
		std::string str =  stream.str();

		char receiveBuff[256];

		int len = webSocket.sendFrame(str.data(), str.size(), Poco::Net::WebSocket::FRAME_TEXT);
		std::cout << "Sent bytes " << len << std::endl;
		int flags=0;

		int rlen = webSocket.receiveFrame(receiveBuff,256,flags);
		std::cout << "Received bytes " << rlen << std::endl;
		std::cout << receiveBuff << std::endl;

		webSocket.close();
    } 
	catch (std::exception &e) 
	{
        std::cout << "Exception " << e.what();
    }

	return true;
}

推荐答案

这似乎是一个Poco错误。您可以通过 http://pocoproject.org/ 使用Poc论坛更快地得到答案[ ^ ]。
This would appear to be a Poco error. You will probably get an answer quicker by using the Poc forum at http://pocoproject.org/[^].


这篇关于C ++ SSL WebSocket连接到CoinBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆