websocket 无法使 Package 器binance.net工作

m0rkklqb  于 2023-10-20  发布在  .NET
关注(0)|答案(3)|浏览(145)

我已经从https://github.com/JKorf/Binance.Net安装了 Package 器
使用他们的示例代码,我在我的应用程序中有以下内容:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Binance.Net;
using Binance.Net.Enums;
using Binance.Net.Objects;
using Binance.Net.Objects.Spot;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;

namespace binance_stream_user_data_updates
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new BinanceClient(new BinanceClientOptions{
                ApiCredentials = new ApiCredentials("key","secret"),
                BaseAddress = "https://testnet.binance.vision",
                LogVerbosity = LogVerbosity.Debug,
                LogWriters = new List<TextWriter> { Console.Out }
            });

            var startResult = client.Spot.UserStream.StartUserStream();

            if(!startResult.Success)
                throw new Exception($"Failed to start user stream: {startResult.Error}");

            var socketClient = new BinanceSocketClient();

            socketClient.Spot.SubscribeToUserDataUpdates(startResult.Data,
            null,
            data => {
                Console.WriteLine(data.Orders);
                },
            null,
            null);

            Console.ReadLine();

            socketClient.UnsubscribeAll();

        }
    }
}

然后我运行上面的应用程序,它似乎连接并开始等待,如终端中所示:

dotnet run
2021/02/26 20:36:51:863 | Binance    | Debug | Client configuration: LogVerbosity: Debug, Writers: 1, Credentials: Set, BaseAddress: https://testnet.binance.vision/, Proxy: -, RateLimiters: 0, RateLimitBehaviour: Wait, RequestTimeout: 00:00:30
2021/02/26 20:36:51:903 | Binance    | Debug | [1] Creating request for https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:51:911 | Binance    | Debug | [1] Sending GET request to https://testnet.binance.vision/api/v3/time 
2021/02/26 20:36:53:242 | Binance    | Debug | [1] Response received in 1320ms: {"serverTime":1614371813101}
2021/02/26 20:36:53:345 | Binance    | Debug | [2] Creating request for https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:53:346 | Binance    | Debug | [2] Sending GET request to https://testnet.binance.vision/api/v3/time 
2021/02/26 20:36:54:028 | Binance    | Debug | [2] Response received in 681ms: {"serverTime":1614371813881}
2021/02/26 20:36:54:029 | Binance    | Info | Time offset set to 535.073ms
2021/02/26 20:36:54:031 | Binance    | Debug | [3] Creating request for https://testnet.binance.vision/api/v1/userDataStream
2021/02/26 20:36:54:037 | Binance    | Debug | [3] Sending POST request to https://testnet.binance.vision/api/v1/userDataStream with request body 
2021/02/26 20:36:54:732 | Binance    | Debug | [3] Response received in 694ms: {"listenKey":"key"}

然后我发布一个命令
POST https://testnet.binance.vision/api/v3/order?symbol=BNBUSDT&side=SELL&type=MARKET&quantity=0.1&newClientOrderId=my_order_id_201&newOrderRespType=FULL&timestamp=1614370483356&signature=58cfd86cffc626703eac32f14bf0fa2e9af4850fb33974a03d1eee3f666df15f
当我这样做时,从上面的代码运行的WebSocket什么也不输出。知道我哪里做错了吗

ifmq2ha2

ifmq2ha21#

有些事情,必须做,以获得适当的结果:
1.您必须指定BinanceSocketClient的选项
1.在取消订阅之前,您必须等待某些事件
1.在等待期间,您必须使用Postman应用程序(或其他类似程序)和您的凭据添加新订单(或新OCO订单)。(以下代码不包含此内容)
请看下面的代码:

async static Task Main(string[] args)
{
    var client = new BinanceClient(new BinanceClientOptions
    {
        ApiCredentials = new ApiCredentials("key", "secret"),
        BaseAddress = "https://testnet.binance.vision",
        LogVerbosity = LogVerbosity.Debug,
        LogWriters = new List<TextWriter> { Console.Out }
    });

    var startResult = client.Spot.UserStream.StartUserStream();

    if (!startResult.Success)
        throw new Exception($"Failed to start user stream: {startResult.Error}");

    var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
    {
        ApiCredentials = new ApiCredentials("key", "secret"),
        BaseAddress = "wss://testnet.binance.vision",
        LogVerbosity = LogVerbosity.Debug,
        LogWriters = new List<TextWriter> { Console.Out }
    });

    var stop = false;
    var subscribeResponse = socketClient.Spot.SubscribeToUserDataUpdates(
        startResult.Data,
        data =>
        {
            stop = true;
            Console.WriteLine($"Order updated: {data}");
        },
        data =>
        {
            stop = true;
            Console.WriteLine($"OCO Orders updated: {data.Orders}");
        },
        null,
        data =>
        {
            stop = true;
            Console.WriteLine($"Balance updated: ${data}");
        });
    
    if (subscribeResponse.Success)
    {
        while (!stop)
        {
            await Task.Delay(100);
        }
    }

    await socketClient.UnsubscribeAll();
}
niknxzdl

niknxzdl2#

您的socketClient在订阅后立即取消订阅事件。
你得换一下线路

socketClient.UnsubscribeAll();
Console.ReadLine();

订阅事件,直到从控制台读取行。

ldxq2e6h

ldxq2e6h3#

在您提供的代码中,您使用测试网URL创建了一个带有选项的BinanceClient,然后创建了一个带有默认选项的BinanceSocketClient,该默认选项将使用wss://stream.binance.com:9443/作为基本URL。
但是如果你使用的是测试网络,那么你就必须使用相应的wss://testnet.binance.vision/ Web Socket URL;否则,您在真实的网络中订阅了一个不起作用的密钥,它不会通知您在测试网络中发生的订单更新。
只需像这样创建BinanceSocketClient示例:

socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
{
    BaseAddress = "wss://testnet.binance.vision",
    LogWriters = new List<TextWriter> { Console.Out }
});

相关问题