示例#1
0
        public SocketV2Client(SocketOptions opt)
        {
            options  = opt;
            _options = new PureWebSocketOptions
            {
                IgnoreCertErrors    = true,
                MyReconnectStrategy = new ReconnectStrategy(2000, 4000, 20)
            };

            Init();
        }
示例#2
0
        public SocketClient(SocketOptions socketOptions)
        {
            var transport = "websocket";
            var url       = socketOptions.Url;
            var options   = new IO.Options
            {
                Secure      = true,
                Transports  = ImmutableList.Create <string>(transport),
                Upgrade     = false,
                AutoConnect = false,
            };

            _socket = IO.Socket(url, options);

            _socket.On("connect", () =>
            {
                IsOpen = true;
                if (_connectActions != null && _connectActions.Any())
                {
                    foreach (var action in _connectActions)
                    {
                        try
                        {
                            action.Invoke();
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    _connectActions.Clear();
                }
                Connected?.Invoke(this, new SocketEventArgs());
            });

            _socket.On("disconnect", () =>
            {
                IsOpen = false;
                Disconnected?.Invoke(this, new SocketEventArgs());
            });

            _socket.On(TickerEvent, (data) =>
            {
                var tickerArgs  = new TickerEventArgs();
                var jsonData    = (data ?? "").ToString();
                tickerArgs.Data = jsonData;
                try
                {
                    var ticker        = JsonConvert.DeserializeObject <TickerData>(jsonData);
                    tickerArgs.Ticker = ticker;
                }
                catch (Exception ex)
                {
                    tickerArgs.Error = $"{ex.Message} {ex.StackTrace}";
                }
                _tickerChanged?.Invoke(this, tickerArgs);
            });

            _socket.On(OrderBookEvent, (data) =>
            {
                var orderArgs  = new OrderBookEventArgs();
                var jsonData   = (data ?? "").ToString();
                orderArgs.Data = jsonData;
                try
                {
                    orderArgs.OrderBook = JsonConvert.DeserializeObject <OrderBookData>(jsonData);
                }
                catch (Exception ex)
                {
                    orderArgs.Error = $"{ex.Message} {ex.StackTrace}";
                }
                _orderBookChanged?.Invoke(this, orderArgs);
            });

            _socket.On(MarketTradeEvent, (data) =>
            {
                var marketArgs  = new MarketTradeEventArgs();
                var jsonData    = (data ?? "").ToString();
                marketArgs.Data = jsonData;
                try
                {
                    marketArgs.MarketTrade = JsonConvert.DeserializeObject <MarketTradeData>(jsonData);
                }
                catch (Exception ex)
                {
                    marketArgs.Error = $"{ex.Message} {ex.StackTrace}";
                }
                _marketTradeChanged?.Invoke(this, marketArgs);
            });
        }