Example #1
0
        async Task <bool> ReadMessage()
        {
            WebSocketReceiveResult result;
            var message = new ArraySegment <byte>(new byte[4096]);

            try
            {
                do
                {
                    result = await _ws.ReceiveAsync(message, _cancelationTokenSource.Token).ConfigureAwait(false);

                    if (result.MessageType != WebSocketMessageType.Text)
                    {
                        break;
                    }
                    var messageBytes = message.Skip(message.Offset).Take(result.Count).ToArray();
                    var messageStr   = Encoding.UTF8.GetString(messageBytes);
                    if (messageStr == "ping")
                    {
                        await _ws.SendAsync(StringToByte("pong"), WebSocketMessageType.Text, true, _cancelationTokenSource.Token).ConfigureAwait(false);

                        continue;
                    }
                    if (messageStr == "pong")
                    {
                        await _ws.SendAsync(StringToByte("ping"), WebSocketMessageType.Text, true, _cancelationTokenSource.Token).ConfigureAwait(false);

                        continue;
                    }
                    if (!MatchId.TryParse(messageStr, out MatchId matchId))
                    {
                        continue;
                    }
                    var match = await _client.GetMatchAsync(matchId, _deviceToSubscribe).ConfigureAwait(false);

                    MatchReceived?.Invoke(this, new MatchReceivedEventArgs(_deviceToSubscribe, MatchChannel.Websocket, new List <Match> {
                        match
                    }));
                }while (!result.EndOfMessage);
                return(true);
            }
            catch (WebSocketException e)
            {
                LastException = e;
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Gets the match async.
        /// </summary>
        /// <returns>The match async.</returns>
        /// <param name="matchId">Match identifier.</param>
        /// <param name="device">Device.</param>
        public async Task <Match> GetMatchAsync(MatchId matchId, Device device = null)
        {
            if (!MainDeviceSet)
            {
                throw new MatchmoreException("Main nor optional device is not ready");
            }

            var usedDevice = device ?? _state.MainDevice;

            try
            {
                return(await _client.GetMatchAsync(usedDevice.Id, matchId.ToString()).ConfigureAwait(false));
            }

            catch (SwaggerException e) when(e.Message == "Match not found" || e.StatusCode == 404)
            {
                return(null);
            }
        }