我与 binanceApi 建立了连接,但我遇到了断开连接的问题,API 断开连接,我不知道原因,我想保持连接处于活动状态,直到我有订单。我会发布我的代码,也许有人可以帮助我。
我已经在 localhost 上创建了另外两个带有 websocketsharp 的项目来测试并且所有断开连接都正常触发。
https://pastebin.com/edit/2Mbh1X4p
public class WSMonitorada
{
public WebSocket ws;
public Usuario User;
public Timer timerKeepAlive = new Timer();
public WSMonitorada(Usuario user, string key)
{
timerKeepAlive.Interval = TimeSpan.FromMinutes(15).TotalMilliseconds;
timerKeepAlive.Elapsed += (object source, ElapsedEventArgs e) =>
{
BinanceUserDataStream.KeepAlive(User.BinanceAPIKey, user.BinanceAPISecret);
};
timerKeepAlive.Start();
ws = new WebSocket("wss://stream.binance.com:9443/ws/" + key);
ws.WaitTime = TimeSpan.FromSeconds(5);
ws.Log.Level = LogLevel.Trace;
ws.Log.File = "C:\\LogConexao\\" + user.nome + ".txt";
//log file never show close event
ws.OnOpen += (sender, e) =>
{
//logic here wors perfect
};
ws.EmitOnPing = true;
ws.OnMessage += (sender, e) =>
{
if (e.IsPing)
{
ws.Ping();
return;
}
//logic here wors perfect
}
ws.OnClose += (sender, e) =>
{
// i have a logic here to analyse if i have a opened order and reconnect again but event never fire
ws.Connect();
};
ws.Connect();
}
}