Exemple #1
0
		public static async Task<Chat> PostChatMessage(int chatId, string message) {
			var chat = new Chat();

			//var authenticatedProfile = await Common.StorageService.RetrieveObjectAsync<Profile>("Profile");

			using (var httpClient = new HttpClient()) {
				var apiKey = Common.StorageService.LoadSetting("ApiKey");
				var apiUrl = Common.StorageService.LoadSetting("ApiUrl");
				var profileToken = Common.StorageService.LoadSetting("ProfileToken");

				httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
				httpClient.DefaultRequestHeaders.Add("token", apiKey);
				httpClient.DefaultRequestHeaders.Add("api-version", "2");
				httpClient.DefaultRequestHeaders.Add("profileToken", profileToken);

				var criteria = new NewChatMessageCriteria {
					ChatId = chatId,
					Message = message
				};

				var uri = new Uri(apiUrl + "/api/chat/message");
				var queryString = new HttpStringContent(JsonConvert.SerializeObject(criteria), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

				HttpResponseMessage response = await httpClient.PostAsync(uri, queryString);
				string json = await response.Content.ReadAsStringAsync();
				json = json.Replace("<br>", Environment.NewLine);
				chat = JsonConvert.DeserializeObject<Chat>(json);
			}

			return chat;
		}
		protected async override void OnNavigatedTo(NavigationEventArgs e)
		{
			var criteria = (ChatDetailPageCriteria)e.Parameter;
			if (criteria != null)
			{
				if (criteria.Chat != null)
				{
					ChatObject = criteria.Chat;
				}
				if (criteria.ChatId > 0 && ChatObject == null)
				{
					//_cts = new CancellationTokenSource();
					//CancellationToken token = _cts.Token;

					//try
					//{
					ChatObject = await Chat.GetChatByIdAsync(criteria.ChatId);
					//}
					//catch (Exception) { }
					//finally
					//{
					//	_cts = null;
					//}
				}
			}
			if (ChatObject != null)
			{
				foreach (var message in ChatObject.Messages)
				{
					ChatMessageList.Add(message);
				}
			}
		}
Exemple #3
0
		public static async Task<Chat> GetChatByIdAsync(int chatId) {
			var chat = new Chat();

			using (var httpClient = new Windows.Web.Http.HttpClient()) {
				var apiKey = Common.StorageService.LoadSetting("ApiKey");
				var apiUrl = Common.StorageService.LoadSetting("ApiUrl");
				var profileToken = Common.StorageService.LoadSetting("ProfileToken");

				httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
				httpClient.DefaultRequestHeaders.Add("token", apiKey);
				httpClient.DefaultRequestHeaders.Add("api-version", "2");
				httpClient.DefaultRequestHeaders.Add("profileToken", profileToken);

				try {

					var url = apiUrl + "/api/chat/" + chatId;

					using (var httpResponse = await httpClient.GetAsync(new Uri(url))) {
						string json = await httpResponse.Content.ReadAsStringAsync();
						json = json.Replace("<br>", Environment.NewLine);
						chat = JsonConvert.DeserializeObject<Chat>(json);
					}
				}
				catch (Exception) { }
			}

			return chat;
		}
		public ChatDetail()
		{
			this.InitializeComponent();

			ChatObject = null;
			ChatMessageList = new ObservableCollection<ChatMessage>();
		}