Пример #1
0
        private async Task <string> FirstSync()
        {
            HttpResponseMessage firstSyncResponse = await _backendHttpClient.Get(
                $"/_matrix/client/r0/sync?filter={_client.FilterId}", true);

            try
            {
                firstSyncResponse.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                if (ex is HttpRequestException || ex is NullReferenceException)
                {
                    Console.WriteLine("Initial Sync failed");
                    return(null);
                }

                throw;
            }

            string firstSyncResponseContents = await firstSyncResponse.Content.ReadAsStringAsync();

            JObject firstSyncJObject = JObject.Parse(firstSyncResponseContents);

            return((string)firstSyncJObject["next_batch"]);
        }
        private async Task <bool> SendMessageRequest(JObject jsonContent)
        {
            if (RoomId == null && RoomAlias == null)
            {
                throw new NullReferenceException("Both RoomId and RoomAlias are null");
            }

            if (string.IsNullOrWhiteSpace(RoomId))
            {
                HttpResponseMessage getRoomIdResponse = await _backendHttpClient.Get($"/_matrix/client/r0/directory/room/{HttpUtility.UrlEncode(RoomAlias)}", false);

                try
                {
                    getRoomIdResponse.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    if (ex is HttpRequestException || ex is NullReferenceException)
                    {
                        Console.WriteLine("Failed to get room ID from room alias");
                        return(false);
                    }

                    throw;
                }

                string getRoomIdResponseContent = await getRoomIdResponse.Content.ReadAsStringAsync();

                JObject roomIdJObject = JObject.Parse(getRoomIdResponseContent);
                RoomId = (string)roomIdJObject["room_id"];
            }

            HttpResponseMessage sendResponse = await _backendHttpClient.Post(
                $"/_matrix/client/r0/rooms/{HttpUtility.UrlEncode(RoomId)}/send/m.room.message", true, jsonContent);

            try
            {
                sendResponse.EnsureSuccessStatusCode();
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is HttpRequestException || ex is NullReferenceException)
                {
                    return(false);
                }

                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Contact the sync endpoint in a fire and forget background task
        /// </summary>
        internal async Task Sync(MatrixClient client, CancellationToken syncCancellationToken)
        {
            _client            = client;
            _backendHttpClient = new MatrixHttp(client.HomeServer, client.AccessToken);

            string nextBatch = string.Empty;

            while (string.IsNullOrWhiteSpace(nextBatch) && !syncCancellationToken.IsCancellationRequested)
            {
                nextBatch = await FirstSync();

                await Task.Delay(2000, syncCancellationToken);
            }

            while (!syncCancellationToken.IsCancellationRequested)
            {
                HttpResponseMessage syncResponseMessage = await _backendHttpClient.Get(
                    $"/_matrix/client/r0/sync?filter={_client.FilterId}&since={nextBatch}", true);

                try
                {
                    syncResponseMessage.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    if (ex is HttpRequestException || ex is NullReferenceException)
                    {
                        Console.WriteLine("Sync failed");
                        await Task.Delay(2000, syncCancellationToken);

                        continue;
                    }

                    throw;
                }

                string syncResponseMessageContents = await syncResponseMessage.Content.ReadAsStringAsync();

                JObject syncResponseJObject = JObject.Parse(syncResponseMessageContents);

                nextBatch = (string)syncResponseJObject["next_batch"];

                SyncChecks(syncResponseJObject);

                await Task.Delay(2000, syncCancellationToken);
            }
        }