mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-11 01:18:50 +00:00
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:
parent
fcc1edff3e
commit
20d24b8738
@ -101,17 +101,24 @@ public class ApiClient
|
|||||||
await SetAuthenticationAsync();
|
await SetAuthenticationAsync();
|
||||||
var count = 0; //
|
var count = 0; //
|
||||||
var requestUrl = $"{_httpClient.BaseAddress}{url}";
|
var requestUrl = $"{_httpClient.BaseAddress}{url}";
|
||||||
var response = await _httpClient.PostAsJsonAsync(requestUrl, obj);
|
var jsonBody = JsonConvert.SerializeObject(obj);
|
||||||
using var responseStream = await response.Content.ReadAsStreamAsync();
|
|
||||||
using var reader = new StreamReader(responseStream, Encoding.UTF8);
|
|
||||||
|
|
||||||
SearchEventType eventType = SearchEventType.Message;
|
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||||
while (!cancellationToken.IsCancellationRequested)
|
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; //
|
count += 1; //
|
||||||
if (count >=5 ){ //
|
if (count >=5 ){ //
|
||||||
break; //
|
yield break; //
|
||||||
}; //
|
}; //
|
||||||
if (jsonChunk == null) continue;
|
if (jsonChunk == null) continue;
|
||||||
if (jsonChunk.StartsWith("event: "))
|
if (jsonChunk.StartsWith("event: "))
|
||||||
|
@ -39,10 +39,12 @@ public partial class Chat : ComponentBase
|
|||||||
private string name = "";
|
private string name = "";
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
var input = _searchServise.FirstMessage;
|
var input = _searchServise.FirstMessage;
|
||||||
|
|
||||||
if (input!=null){
|
if (input != null)
|
||||||
|
{
|
||||||
|
|
||||||
await LoadMessages();
|
await LoadMessages();
|
||||||
|
|
||||||
@ -70,10 +72,14 @@ public partial class Chat : ComponentBase
|
|||||||
await UpdateSideMenu(wishlistId);
|
await UpdateSideMenu(wishlistId);
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
}else{
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
await LoadMessages();
|
await LoadMessages();
|
||||||
}
|
}
|
||||||
}catch(Exception ex){
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
Console.WriteLine($"Error OnInitializedAsync: {ex.Message}");
|
Console.WriteLine($"Error OnInitializedAsync: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +88,8 @@ public partial class Chat : ComponentBase
|
|||||||
|
|
||||||
private async Task LoadMessages()
|
private async Task LoadMessages()
|
||||||
{
|
{
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
string wishlistId = chatId;
|
string wishlistId = chatId;
|
||||||
|
|
||||||
var request = new GraphQLRequest
|
var request = new GraphQLRequest
|
||||||
@ -137,7 +144,9 @@ public partial class Chat : ComponentBase
|
|||||||
Messages.Reverse();
|
Messages.Reverse();
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
|
|
||||||
}catch(Exception ex){
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
Console.WriteLine($"Error : {ex.Message}");
|
Console.WriteLine($"Error : {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,7 +158,8 @@ public partial class Chat : ComponentBase
|
|||||||
JSRuntime.InvokeVoidAsync("clearInput");
|
JSRuntime.InvokeVoidAsync("clearInput");
|
||||||
isWaitingForResponse = true;
|
isWaitingForResponse = true;
|
||||||
|
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
messageCreateDto = new MessageCreateDto { Text = inputMessage }; ;
|
messageCreateDto = new MessageCreateDto { Text = inputMessage }; ;
|
||||||
Message = new Messages();
|
Message = new Messages();
|
||||||
Message.Text = inputMessage;
|
Message.Text = inputMessage;
|
||||||
@ -186,9 +196,8 @@ public partial class Chat : ComponentBase
|
|||||||
Match match = regex.Match(input);
|
Match match = regex.Match(input);
|
||||||
string result = match.Groups[1].Value;
|
string result = match.Groups[1].Value;
|
||||||
|
|
||||||
if(sseEvent.Event == SearchEventType.Message){
|
if (sseEvent.Event == SearchEventType.Message)
|
||||||
|
{
|
||||||
|
|
||||||
if (first)
|
if (first)
|
||||||
{
|
{
|
||||||
Messages[lengt - 1].Text = result;
|
Messages[lengt - 1].Text = result;
|
||||||
@ -201,7 +210,9 @@ public partial class Chat : ComponentBase
|
|||||||
|
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
} else if(sseEvent.Event == SearchEventType.Product){
|
}
|
||||||
|
else if (sseEvent.Event == SearchEventType.Product)
|
||||||
|
{
|
||||||
|
|
||||||
string pattern = "[\\\\\"]";
|
string pattern = "[\\\\\"]";
|
||||||
|
|
||||||
@ -209,14 +220,19 @@ public partial class Chat : ComponentBase
|
|||||||
|
|
||||||
Products.Add(input);
|
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);
|
Suggestion.Add(result);
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if (Products.Count != 0)
|
||||||
if(Products.Count!=0) {
|
{
|
||||||
string n = name;
|
string n = name;
|
||||||
_searchServise.SetProducts(Products);
|
_searchServise.SetProducts(Products);
|
||||||
Products = null;
|
Products = null;
|
||||||
@ -224,7 +240,10 @@ public partial class Chat : ComponentBase
|
|||||||
Navigation.NavigateTo(url);
|
Navigation.NavigateTo(url);
|
||||||
}
|
}
|
||||||
isWaitingForResponse = false;
|
isWaitingForResponse = false;
|
||||||
} catch(Exception ex){
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
Console.WriteLine($"Error : {ex.Message}");
|
Console.WriteLine($"Error : {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user