Reputation: 15935
I have installed the wrapper from https://github.com/JKorf/Binance.Net
Using their example code, I have the following in my app:
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();
}
}
}
I then run the above app which seems to connect and starts waiting, as the following is shown in the terminal:
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"}
I then post an order
When I do that, the websocket running from the above code outputs nothing. Any idea what I am doing wrong?
Upvotes: 2
Views: 8062
Reputation: 18023
In your provided code you create a BinanceClient
with options using the testnet URL, and then you create a BinanceSocketClient
with default options that will use wss://stream.binance.com:9443/
as the base URL.
But if you use the test network, then you have to use the corresponding wss://testnet.binance.vision/
web socket URL; otherwise, you subscribe to a nonfunctional listenKey in the real network, which will not inform you about the order updates that happen in the test network.
Just create the BinanceSocketClient
instance like this:
socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
{
BaseAddress = "wss://testnet.binance.vision",
LogWriters = new List<TextWriter> { Console.Out }
});
Upvotes: 0
Reputation: 2017
There are things, that have to be done to get a proper result:
BinanceSocketClient
Please have a look to the following code:
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();
}
Upvotes: 5
Reputation: 1417
Your socketClient
is unsubscribed from events immediately after subscribing.
You need to swap the lines
socketClient.UnsubscribeAll();
Console.ReadLine();
to have a subscription to events until the line from the console is read.
Upvotes: 1