Skip to content

Commit 6cc2d2c

Browse files
authored
fix Crypto.com trade stream errors (#760)
- respond to heartbeat - turn off KeepAlive - warn instead of throwing exception
1 parent 658b4f5 commit 6cc2d2c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/ExchangeSharp/API/Exchanges/CryptoCom/ExchangeCryptoComApi.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
4444
{
4545
marketSymbols = new string[] { "" };
4646
}
47-
return await ConnectPublicWebSocketAsync("/market", async (_socket, msg) =>
47+
var ws = await ConnectPublicWebSocketAsync("/market", async (_socket, msg) =>
4848
{
4949
/*{
5050
{{
@@ -74,9 +74,17 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
7474
throw new APIException(token["code"].ToStringInvariant() + ": " + token["message"].ToStringInvariant());
7575
}
7676
else if (token["method"].ToStringInvariant() == "public/heartbeat")
77-
{
77+
{ /* For websocket connections, the system will send a heartbeat message to the client every 30 seconds.
78+
* The client must respond back with the public/respond-heartbeat method, using the same matching id, within 5 seconds, or the connection will break. */
79+
var hrResponse = new
80+
{
81+
id = token["id"].ConvertInvariant<long>(),
82+
method = "public/respond-heartbeat",
83+
};
84+
await _socket.SendMessageAsync(hrResponse);
85+
7886
if (token["message"].ToStringInvariant() == "server did not receive any client heartbeat, going to disconnect soon")
79-
throw new APIException(token["code"].ToStringInvariant() + ": " + token["message"].ToStringInvariant());
87+
Logger.Warn(token["code"].ToStringInvariant() + ": " + token["message"].ToStringInvariant());
8088
}
8189
else if (token["method"].ToStringInvariant() == "subscribe" && token["result"] != null)
8290
{
@@ -115,6 +123,7 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
115123
var subscribeRequest = new
116124
{
117125
// + consider using id field in the future to differentiate between requests
126+
//id = new Random().Next(),
118127
method = "subscribe",
119128
@params = new
120129
{
@@ -124,6 +133,8 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
124133
};
125134
await _socket.SendMessageAsync(subscribeRequest);
126135
});
136+
ws.KeepAlive = new TimeSpan(0); // cryptocom throws bad request empty content msgs w/ keepalives
137+
return ws;
127138
}
128139
}
129140
public partial class ExchangeName { public const string CryptoCom = "CryptoCom"; }

0 commit comments

Comments
 (0)