Skip to content

Commit e62251a

Browse files
committed
OKGroup: added websocket ping to prevent connection timeout
- mostly affects OKCoin given sparse trading there - renamed ExchangeOKExAPI.cs file capitalization
1 parent 53cff85 commit e62251a

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

ExchangeSharp/API/Exchanges/OKGroup/OKGroupCommon.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The above copyright notice and this permission notice shall be included in all c
1616
using System.Linq;
1717
using System.Text;
1818
using System.Threading.Tasks;
19+
using System.Threading;
1920

2021
// different namespace for this since we don't want this to be easily accessible publically
2122
namespace ExchangeSharp.OKGroup
@@ -643,28 +644,37 @@ private ExchangeOrderResult ParseOrder(JToken token)
643644

644645
private IWebSocket ConnectWebSocketOkex(Func<IWebSocket, Task> connected, Func<IWebSocket, string, string[], JToken, Task> callback, int symbolArrayIndex = 3)
645646
{
646-
return ConnectWebSocket(string.Empty, async (_socket, msg) =>
647+
Timer pingTimer = null;
648+
return ConnectWebSocket(url: string.Empty, messageCallback: async (_socket, msg) =>
647649
{
648-
// https://github.com/okcoin-okex/API-docs-OKEx.com/blob/master/README-en.md
649-
// All the messages returning from WebSocket API will be optimized by Deflate compression
650-
JToken token = JToken.Parse(msg.ToStringFromUTF8Deflate());
651-
650+
// https://github.com/okcoin-okex/API-docs-OKEx.com/blob/master/README-en.md
651+
// All the messages returning from WebSocket API will be optimized by Deflate compression
652+
var msgString = msg.ToStringFromUTF8Deflate();
653+
if (msgString == "pong")
654+
{ // received reply to our ping
655+
return;
656+
}
657+
JToken token = JToken.Parse(msgString);
652658
var eventProperty = token["event"]?.ToStringInvariant();
653659
if (eventProperty != null)
654660
{
655-
if (eventProperty == "error")
656-
{
661+
if (eventProperty == "error")
662+
{
657663
Logger.Info("Websocket unable to connect: " + token["message"]?.ToStringInvariant());
658664
return;
659-
}
660-
661-
if (eventProperty == "subscribe" && token["channel"] != null)
662-
{
663-
return;
664-
}
665+
}
666+
else if (eventProperty == "subscribe" && token["channel"] != null)
667+
{ // subscription successful
668+
if (pingTimer == null)
669+
{
670+
pingTimer = new Timer(callback: async s => await _socket.SendMessageAsync("ping"),
671+
state: null, dueTime: 0, period: 15000); // send a ping every 15 seconds
672+
}
673+
return;
674+
}
675+
else return;
665676
}
666-
667-
if (token["table"] != null)
677+
else if (token["table"] != null)
668678
{
669679
var data = token["data"];
670680
foreach (var dataRow in data)
@@ -673,10 +683,13 @@ private IWebSocket ConnectWebSocketOkex(Func<IWebSocket, Task> connected, Func<I
673683
await callback(_socket, marketSymbol, null, dataRow);
674684
}
675685
}
676-
}, async (_socket) =>
677-
{
678-
await connected(_socket);
679-
});
686+
}, connectCallback: async (_socket) => await connected(_socket)
687+
, disconnectCallback: s =>
688+
{
689+
pingTimer.Dispose();
690+
pingTimer = null;
691+
return Task.CompletedTask;
692+
});
680693
}
681694

682695
private IWebSocket ConnectPrivateWebSocketOkex(Func<IWebSocket, Task> connected, Func<IWebSocket, string, string[], JToken, Task> callback, int symbolArrayIndex = 3)

ExchangeSharp/API/Exchanges/_Base/ExchangeAPIExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The above copyright notice and this permission notice shall be included in all c
1414
using System.Collections.Concurrent;
1515
using System.Collections.Generic;
1616
using System.Threading.Tasks;
17-
using ExchangeSharp.API.Exchanges.Kraken.Models;
17+
using ExchangeSharp.Kraken;
1818
using ExchangeSharp.Binance;
1919
using ExchangeSharp.Bitstamp;
2020
using ExchangeSharp.Coinbase;

ExchangeSharp/Utility/ClientWebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public Task<bool> SendMessageAsync(object message)
312312
});
313313
return Task.FromResult<bool>(true);
314314
}
315-
return Task.FromResult<bool>(false);
315+
return Task.FromResult<bool>(false);
316316
}
317317

318318
private void QueueActions(params Func<IWebSocket, Task>[] actions)

0 commit comments

Comments
 (0)