# binance_cpp_websocket **Repository Path**: optionsquant/binance_cpp_websocket ## Basic Information - **Project Name**: binance_cpp_websocket - **Description**: 使用C++连接binance行情测试 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-14 - **Last Updated**: 2025-11-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Binance USDS-M 期货 Orderbook Demo(C++ / uWebSockets / Boost.Beast) 本项目提供 4 个可执行文件,覆盖 Binance USDS-Margined Futures 市场数据的不同接入方式,并提供统一的延迟统计与运行脚本: - `binance_orderbook_demo.exe`(uWebSockets 基础版):单流端点 `/ws`,打印原始 JSON 与简单延迟 - `binance_orderbook_uws_opt.exe`(uWebSockets 优化版):支持 `/ws` 与 `/stream`,统一延迟计算(含服务器时间校准),统计更丰富 - `binance_orderbook_beast_demo.exe`(Boost.Beast 版):使用 `/stream` 端点,通过 SUBSCRIBE 订阅并计算延迟 - `binance_orderbook_beast_opt.exe`(Boost.Beast 优化版):在 `beast_demo` 基础上增强统计与一致化算法 **统一运行脚本**:`run.bat` 会先编译,再按同样参数运行 4 个可执行文件,并生成独立日志。 ## 功能 - 连接 `wss://fstream.binance.com` 获取 Partial Book Depth(`depth@100ms`) - 统一的延迟统计:以消息 `E` 字段(服务器事件时间)为基准,计算本地到达延迟 - 服务器时间校准(优化版):通过 HTTP 获取 `serverTime`,修正本地系统时间偏差 - 端点选择: - 单流端点 `/ws/@depth@100ms` - 组合端点 `/stream`(连接后发送 SUBSCRIBE 订阅多个流) ## 依赖安装(Windows,推荐 vcpkg) 1. 安装 vcpkg(如未安装):https://github.com/microsoft/vcpkg 2. 安装依赖库(至少): - `vcpkg install uwebsockets` - `vcpkg install openssl` > vcpkg 提供 `uwebsockets` 的 CMake config 包,可 `find_package(uwebsockets CONFIG REQUIRED)` 并链接 `uwebsockets::uwebsockets`。 ## 构建 ```powershell # 进入项目根目录 cd e:\1workspace\1cpp_code\1subscribe_orderbook\3sub_binance_trae # 生成与编译(使用 vcpkg 工具链) cmake -S . -B build-vcpkg -DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Release cmake --build build-vcpkg --config Release # 或使用一键脚本(构建并运行) run.bat BTCUSDT 5 100 10 ws ``` > 如未设置 `VCPKG_ROOT`,可在脚本中设置为你的 vcpkg 安装目录。 ## 运行(推荐 `run.bat`) `run.bat` 参数:`SYMBOL LEVELS INTERVAL DURATION ENDPOINT(ws|stream)` 示例: - `run.bat`(使用默认:`BTCUSDT 5 100 10 ws`) - `run.bat BTCUSDT 5 100 10 ws` - `run.bat BTCUSDT 5 100 10 stream` 运行后会在 `logs/` 目录生成 4 个日志文件: - `beast_.log` - `beast_opt_.log` - `uws_.log` - `uws_opt_.log` ## 单独运行各可执行文件 可执行文件位于:`build-vcpkg/Release/` - `binance_orderbook_demo.exe`(uWS 基础版,单流 `/ws`) - 参数:`--symbol=` `--levels=<5|10|20>` `--interval=100` `--duration=` - 示例:`binance_orderbook_demo.exe --symbol=ETHUSDT --levels=10 --interval=100 --duration=10` - `binance_orderbook_uws_opt.exe`(uWS 优化版,支持 `/ws` 与 `/stream`) - 参数:`--symbol=` `--levels=<5|10|20>` `--interval=100` `--endpoint=` - 示例:`binance_orderbook_uws_opt.exe --symbol=btcusdt --levels=5 --interval=100 --endpoint=stream` - `binance_orderbook_beast_demo.exe`(Beast 组合端点 `/stream`) - 参数:`--symbol=` `--stream=` `--duration=` - 示例:`binance_orderbook_beast_demo.exe --symbol=BTCUSDT --stream=depth@100ms --duration=60` - `binance_orderbook_beast_opt.exe`(Beast 优化版) - 参数同 `beast_demo`,统计更丰富(平均/中位/分位/标准差) ## 延迟计算与统一性说明 - 基准:消息 JSON 的 `E` 字段(事件时间,毫秒) - 到达时间:本地 `system_clock::now()` - 校准:优化版在程序启动时通过 HTTP 获取 `serverTime`,计算 `clock_offset_ms = serverTime - localNow`,延迟为 `(now_ms + clock_offset_ms) - event_ms` - 日志频率:运行中每 200 条打印一次统计;关闭或运行结束打印完整报告 ## 端点差异 - `/ws`:直接连接单一流,形如:`wss://fstream.binance.com/ws/@depth@100ms` - `/stream`:连接后发送 SUBSCRIBE 消息,可订阅多个流;示例中用于订阅 `depth@100ms` ## 常见问题与排查 - OpenSSL 未找到:请使用 vcpkg 工具链配置并确保 `openssl` 已安装 - 程序运行找不到 DLL:将 `%VCPKG_ROOT%\installed\x64-windows\bin` 加入 `PATH` - TLS 握手失败:检查系统证书与 vcpkg OpenSSL 安装;演示程序可能关闭了验证(`verify_none`),生产环境务必开启证书校验并加载根证书 - 收不到消息(`/stream`):确认已在 `open` 回调发送 SUBSCRIBE 消息订阅目标深度流 ## 参考 - uWebSockets 使用与安装建议(Windows/vcpkg):[How to use uWebsockets from Visual Studio C++?](https://stackoverflow.com/questions/61833538/how-to-use-uwebsockets-from-visual-studio-c) - uWebSockets 官方仓库:[uNetworking/uWebSockets](https://github.com/uNetworking/uWebSockets) - Boost.Beast WebSocket/TLS 示例:[C or C++ websocket client working example](https://stackoverflow.com/questions/69051106/c-or-c-websocket-client-working-example)