/// <summary> /// Send data subscription message /// </summary> /// <param name="mode">Mode</param> /// <param name="tokens">Symbol tokens</param> /// <param name="isSubscribe">Is data subscribe or unstubscribed. Default is subscribed</param> /// <returns></returns> private async Task SendMessageAsync(string mode, int[] tokens, bool isSubscribe = true) { if (tokens == null) { try { this.logger?.OnException(new ArgumentNullException("token is null")); } catch (Exception ex) { } return; } if (string.IsNullOrWhiteSpace(mode)) { try { this.logger?.OnException(new ArgumentNullException("mode is null")); } catch (Exception ex) { } return; } Message<int[]> subscribeMsg = new Message<int[]>() { a = isSubscribe ? Message.subscribe : Message.unsubscribe, v = tokens }; string actionString = string.Empty; try { actionString = subscribeMsg.Serialize(); } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } return; } if (string.IsNullOrEmpty(actionString)) { try { this.logger?.OnException(new ArgumentNullException("action json string is null")); } catch (Exception ex) { } return; } try { this.logger?.OnLog(actionString); } catch (Exception ex) { } string modeMsgString = string.Empty; if (isSubscribe) { Message<object[]> modeMsg = new Message<object[]>() { a = Message.mode, v = new object[] { mode, tokens } }; try { modeMsgString = modeMsg.Serialize(); } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } return; } if (string.IsNullOrEmpty(modeMsgString)) { try { this.logger?.OnException(new ArgumentNullException("Mode json string is null")); } catch (Exception ex) { } return; } try { this.logger?.OnLog(modeMsgString); } catch (Exception ex) { } } try { await this.socket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(actionString)), WebSocketMessageType.Text, true, CancellationToken.None).ConfigureAwait(false); //send the subscription msg if (isSubscribe) { await this.socket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(modeMsgString)), WebSocketMessageType.Text, true, CancellationToken.None).ConfigureAwait(false); //send the mode msg } } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } } }
private async void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (this.isTimerBusy) return; this.isTimerBusy = true; if (this.socket?.State != WebSocketState.Open) { try { this.logger?.OnLog($"Connection state = {this.socket?.State}"); } catch (Exception ex) { } } if (this.socket?.State == WebSocketState.Aborted) //if (this.lastUpdateTime.AddSeconds(this.reconnectionTime * 20) < DateTime.UtcNow) //Abort state can take some time. Alt will be check time { OnState(KiteConnectState.ConnectionLost); this.logger?.OnLog($"SocketState ={this.socket?.State} ...."); try { await ConnectAsync().ConfigureAwait(false); } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } } this.isTimerBusy = false; return; } List<Message<int[]>> tmp = new List<Message<int[]>>(); lock (locker) { for (int i = 0; i < this.queue.Count; i++) { tmp.Add(this.queue.Dequeue()); } } for (int i = 0; i < tmp.Count; i++) { if (this.socket?.State != WebSocketState.Open) break; Message<int[]> message = tmp[i]; if (message == null) continue; await this.SendMessageAsync(message.a, message.v, message.IsSubscribe).ConfigureAwait(false); } this.isTimerBusy = false; }