@@ -16,6 +16,7 @@ The above copyright notice and this permission notice shall be included in all c
1616using System . Linq ;
1717using System . Text ;
1818using System . Threading . Tasks ;
19+ using System . Threading ;
1920
2021// different namespace for this since we don't want this to be easily accessible publically
2122namespace 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 )
0 commit comments