Exemplo n.º 1
0
        /// <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
            {
                this.webSocket.Send(actionString);
                if (isSubscribe)
                {
                    this.webSocket.Send(modeMsgString);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    this.logger?.OnException(ex);
                }
                catch (Exception ex1)
                {
                }
            }
        }
        /// <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)
                {

                }
            }
        }