c++ 使用libwebsockets的Binance Futures无法正常工作

lb3vh1jj  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(143)

我正在做一个C++应用程序,它可以通过WebSockets从Binance Futures获取价格信息。为了实现我的目标,我使用了Yair Gadelov的websockets Package 器项目,它基于libwebsockets。这篇文章告诉我们如何使用这个 Package 器:https://yairgadelov.me/websockets-with-c-/这是Github项目:https://github.com/yairgd/websockets
如果我使用main.cpp,它可以正常工作,它连接到Binance spot WebSocket并向我发送我需要的信息。但是如果我把它换成futures流,它就不能连接了:

#include "WebSockets.h"
#include <iostream>
#include <string>

int main(int argc, const char** argv)
{
    WebSockets ws;

    auto func = [](std::string json, std::string protol_name) ->bool {
        std::cout << protol_name << ": " << json << std::endl;
        return true;
    };

    auto runAfterConnection = [&ws](void) ->void {
        ws["futures"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"btcusdt@ticker\"],\"id\": 1}");
    };

    ws.AddProtocol("futures", "fstream.binance.com", "/ws", 9443, func);

    ws.Connect(runAfterConnection);
    ws.Run();
}

我不知道如何测试订阅,但此URL工作正常,并产生我需要的Google Chrome WebSocket测试客户端扩展的信息:wss://fstream.binance.com/ws/btcusdt@ticker
我应该修改什么才能达到相应的效果?

oo7oh9g9

oo7oh9g91#

我想明白了问题的根源是端口。对于特征,不是9443,而是443。所以工作代码看起来像这样:

#include "WebSockets.h"
#include <iostream>
#include <string>

int main(int argc, const char** argv)
{
    WebSockets ws;

    auto func = [](std::string json, std::string protol_name) ->bool {
        std::cout << protol_name << ": " << json << std::endl;
        return true;
    };

    auto runAfterConnection = [&ws](void) ->void {
        ws["futures"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"btcusdt@ticker\"],\"id\": 1}");
    };

    ws.AddProtocol("futures", "fstream.binance.com", "/ws", 443, func);

    ws.Connect(runAfterConnection);
    ws.Run();
}

相关问题