Exemple #1
0
        public async Task <string> SubscribeDiscount()
        {
            byte[] buffer = new byte[20000];
            ArraySegment <byte> segment = new ArraySegment <byte>(buffer);
            WebMessageBase      request = new WebMessageBase("subscription");
            string requestJson          = JsonConvert.SerializeObject(request, Formatting.Indented);
            await _clientDiscountWebSocket.SendAsync(requestJson);

            bool   gotCorrectResponse = false;
            string mesg = "";

            while (!gotCorrectResponse)
            {
                WebSocketReceiveResult result = _clientDiscountWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    _clientDiscountWebSocket.Disconnect();
                    throw new Exception("Disconnected. Close message");
                }
                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        _clientDiscountWebSocket.Disconnect();
                        throw new Exception("Disconnected. Buffer overloaded");
                    }
                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = _clientDiscountWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                    count  += result.Count;
                }
                mesg = Encoding.UTF8.GetString(buffer, 0, count);
                WebMessageBase baseResponse = JsonConvert.DeserializeObject <WebMessageBase>(mesg);
                if (baseResponse.Tag == "subscription")
                {
                    gotCorrectResponse = true;
                    if (baseResponse.Status != MessageStatus.SUCCESS)
                    {
                        throw new Exception(baseResponse.Message);
                    }
                }
            }
            _clientDiscountWebSocket.ActiveMessageLoop = true;
            WebMessageBase response = JsonConvert.DeserializeObject <WebMessageBase>(mesg);

            if (response.Status == MessageStatus.SUCCESS)
            {
                return(response.Message);
            }
            else
            {
                return("Could not subscribe. " + response.Message);
            }
        }
Exemple #2
0
 public async Task SendMessage(string message)
 {
     await _clientWebSocket.SendAsync(message);
 }