Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvokeAsync(StateHasChanged) from StateHasChanged() #239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ismusidhu
Copy link

@ismusidhu ismusidhu commented Aug 28, 2023

Browser was not getting changes promptly. It was getting changes one event late. e.g. if user clicks send button, then it would get pending message from other users and it would still not show the message that was sent by itself which would be shown only after another action from the user.

This correction is taken from https://learn.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr.

@ismusidhu
Copy link
Author

@dotnet-policy-service agree

@@ -40,12 +49,18 @@ else
else
{
<div class="@item.CSS">
@if (!string.IsNullOrWhiteSpace(item.RecieverUsername))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add From and To for easy understand and optimize order like below?

<div class="user">From: @item.Username</div>
@if (!string.IsNullOrWhiteSpace(item.RecieverUsername))
{
    <div class="user">To: @item.RecieverUsername</div>
}
<div class="msg">@item.Body</div>

{
_connectedUsers = userList.ToList();
InvokeAsync(StateHasChanged);
//StateHasChanged();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this?

<div class="user">@item.Username</div>
<div class="msg">@item.Body</div>
</div>
}
}
<hr />
<label for="recipientUsername">Send message to:</label>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a <div> to wrap this Send section, so won't break the rendering.

<div id="sendto">
    <label for="recipientUsername">Send message to:</label>
    <input type="text" id="recipientUsername" @bind="_recipientUsername" />
</div>

@@ -8,22 +11,42 @@ namespace BlazorChat
public class BlazorChatSampleHub : Hub
{
public const string HubUrl = "/chat";
private static ConcurrentDictionary<string, string> _connectedUsers = new ConcurrentDictionary<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about cache Dictionary<UserName, List>()? As always fetch by userId.


public async Task Broadcast(string username, string message)
{
await Clients.All.SendAsync("Broadcast", username, message);
}
public async Task SendUserToUser(string senderUsername, string recipientUsername, string message)
{
var recipientConnectionId = _connectedUsers.FirstOrDefault(u => u.Value == recipientUsername).Key;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache by userId as key so you don't need to find by value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants