Пример #1
0
        private void Init()
        {
            var webSocket = _webSocket = HuobiWebSocketClient.GetWebSocket();

            webSocket.OnMessage += (sender, e) => {
                var data = GZipDecompresser.Decompress(e.RawData);
                if (data.Contains("ping"))
                {
                    webSocket.Send(data.Replace("ping", "pong"));
                }
                else
                {
                    try {
                        var updateEvent = Json.Deserialize <DepthTickUpdateEvent>(data);
                        _cache[updateEvent.Ch] = updateEvent.Tick;
                    }
                    catch (Exception) {
                        // ignored
                    }
                }
            };
            webSocket.OnClose += (sender, e) => _cache.Clear();
            webSocket.OnError += (sender, e) => WebSocketUtils.CloseWebSocket(webSocket);
            webSocket.Connect();
        }
Пример #2
0
        private void SetTimer()
        {
            var timer = new System.Timers.Timer(5000);

            timer.Elapsed += (a, b) => {
                if (_webSocket.IsAlive)
                {
                    return;
                }
                WebSocketUtils.CloseWebSocket(_webSocket);
                _cache.Clear();
                Init();
            };
            timer.AutoReset = true;
            timer.Enabled   = true;
        }
Пример #3
0
        private void Init()
        {
            var webSocket = _webSocket = HuobiWebSocketClient.GetWebSocket();

            webSocket.OnMessage += (sender, e) => {
                var data = GZipDecompresser.Decompress(e.RawData);
                if (data.Contains("ping"))
                {
                    webSocket.Send(data.Replace("ping", "pong"));
                }
                else
                {
                    try {
                        var updateEvent = Json.Deserialize <TrickUpdateEvent>(data);
                        if (!_cache.ContainsKey(updateEvent.Ch))
                        {
                            lock (this) {
                                if (!_cache.ContainsKey(updateEvent.Ch))
                                {
                                    _cache[updateEvent.Ch] = SortedDictionaryFactory.NewSortedDictionary();
                                }
                            }
                        }

                        var ticks = _cache[updateEvent.Ch];
                        ticks[updateEvent.Tick.Id] = updateEvent.Tick;
                        while (ticks.Count > Size)
                        {
                            ticks.Remove(ticks.Last().Key);
                        }
                    }
                    catch (Exception) {
                        // ignored
                    }
                }
            };
            webSocket.OnClose += (sender, e) => _cache.Clear();
            webSocket.OnError += (sender, e) => WebSocketUtils.CloseWebSocket(webSocket);
            webSocket.Connect();
        }
Пример #4
0
        private void Init()
        {
            var webSocket = _webSocket = HuobiWebSocketClient.GetWebSocket();

            webSocket.OnMessage += (sender, e) => {
                var data = GZipDecompresser.Decompress(e.RawData);
                if (data.Contains("ping"))
                {
                    (sender as WebSocket)?.Send(data.Replace("ping", "pong"));
                }
                else
                {
                    try {
                        var updateEvent = Json.Deserialize <TradeTickUpdateEvent>(data);
                        if (!_cache.ContainsKey(updateEvent.Ch))
                        {
                            _cache[updateEvent.Ch] = new ConcurrentQueue <TradeData>();
                        }

                        foreach (var tradeData in updateEvent.Tick.Data)
                        {
                            _cache[updateEvent.Ch].Enqueue(tradeData);
                        }

                        while (_cache[updateEvent.Ch].Count > 100)
                        {
                            _cache[updateEvent.Ch].TryDequeue(out _);
                        }
                    }
                    catch (Exception exception) {
                        Console.WriteLine(exception);
                    }
                }
            };
            webSocket.OnClose += (sender, e) => _cache.Clear();
            webSocket.OnError += (sender, e) => WebSocketUtils.CloseWebSocket(webSocket);
            webSocket.Connect();
        }