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
base: main
Are you sure you want to change the base?
Conversation
@dotnet-policy-service agree |
@@ -40,12 +49,18 @@ else | |||
else | |||
{ | |||
<div class="@item.CSS"> | |||
@if (!string.IsNullOrWhiteSpace(item.RecieverUsername)) |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
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.