SA-240 Display messages writing in realtime

- Change SSE handling to display them as soon as they come
- Change AddNewMessage() to disply messages and suggestions in realtime
This commit is contained in:
shchoholiev 2023-12-16 22:37:38 +00:00
parent fcc1edff3e
commit 20d24b8738
2 changed files with 183 additions and 157 deletions

View File

@ -101,17 +101,24 @@ public class ApiClient
await SetAuthenticationAsync();
var count = 0; //
var requestUrl = $"{_httpClient.BaseAddress}{url}";
var response = await _httpClient.PostAsJsonAsync(requestUrl, obj);
using var responseStream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(responseStream, Encoding.UTF8);
var jsonBody = JsonConvert.SerializeObject(obj);
SearchEventType eventType = SearchEventType.Message;
while (!cancellationToken.IsCancellationRequested)
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
{
var jsonChunk = await reader.ReadLineAsync(cancellationToken);
Content = body
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
using var httpResponse = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
using var streamReader = new StreamReader(await httpResponse.Content.ReadAsStreamAsync(cancellationToken));
var eventType = SearchEventType.Message;
while (!streamReader.EndOfStream)
{
var jsonChunk = await streamReader.ReadLineAsync(cancellationToken);
count += 1; //
if (count >=5 ){ //
break; //
yield break; //
}; //
if (jsonChunk == null) continue;
if (jsonChunk.StartsWith("event: "))

View File

@ -39,10 +39,12 @@ public partial class Chat : ComponentBase
private string name = "";
protected override async Task OnInitializedAsync()
{
try{
try
{
var input = _searchServise.FirstMessage;
if (input!=null){
if (input != null)
{
await LoadMessages();
@ -70,10 +72,14 @@ public partial class Chat : ComponentBase
await UpdateSideMenu(wishlistId);
StateHasChanged();
}else{
}
else
{
await LoadMessages();
}
}catch(Exception ex){
}
catch (Exception ex)
{
Console.WriteLine($"Error OnInitializedAsync: {ex.Message}");
}
@ -82,7 +88,8 @@ public partial class Chat : ComponentBase
private async Task LoadMessages()
{
try{
try
{
string wishlistId = chatId;
var request = new GraphQLRequest
@ -137,7 +144,9 @@ public partial class Chat : ComponentBase
Messages.Reverse();
isLoading = false;
}catch(Exception ex){
}
catch (Exception ex)
{
Console.WriteLine($"Error : {ex.Message}");
}
}
@ -149,8 +158,9 @@ public partial class Chat : ComponentBase
JSRuntime.InvokeVoidAsync("clearInput");
isWaitingForResponse = true;
try{
messageCreateDto = new MessageCreateDto { Text = inputMessage };;
try
{
messageCreateDto = new MessageCreateDto { Text = inputMessage }; ;
Message = new Messages();
Message.Text = inputMessage;
Message.Role = "User";
@ -186,22 +196,23 @@ public partial class Chat : ComponentBase
Match match = regex.Match(input);
string result = match.Groups[1].Value;
if(sseEvent.Event == SearchEventType.Message){
if (sseEvent.Event == SearchEventType.Message)
{
if (first)
{
Messages[lengt-1].Text = result;
Messages[lengt - 1].Text = result;
first = false;
}
else
{
Messages[lengt-1].Text += result;
Messages[lengt - 1].Text += result;
}
StateHasChanged();
} else if(sseEvent.Event == SearchEventType.Product){
}
else if (sseEvent.Event == SearchEventType.Product)
{
string pattern = "[\\\\\"]";
@ -209,14 +220,19 @@ public partial class Chat : ComponentBase
Products.Add(input);
} else if(sseEvent.Event == SearchEventType.Suggestion){
if(Suggestion.Count<3){
}
else if (sseEvent.Event == SearchEventType.Suggestion)
{
if (Suggestion.Count < 3)
{
Suggestion.Add(result);
StateHasChanged();
}
}
}
}
if(Products.Count!=0) {
if (Products.Count != 0)
{
string n = name;
_searchServise.SetProducts(Products);
Products = null;
@ -224,7 +240,10 @@ public partial class Chat : ComponentBase
Navigation.NavigateTo(url);
}
isWaitingForResponse = false;
} catch(Exception ex){
}
catch (Exception ex)
{
Console.WriteLine($"Error : {ex.Message}");
}
}