Neeyo
Neeyo
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Thank you, really, for a lot of your time
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Okay, I believe I will do better next time :sadge:
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
I am familiar
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
You will get angry but he has to in my case xD Its fault of my naming, group right now as it is named in my code in more of a Room, user can have access to many rooms, for example there are Rooms: "Gardening", "Cars", "Coding". User connecting to websocket can joint either of those and has to specify which one he wants to join, thats why there is query ?groupName in url
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Thank you, you told me a lot
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Okay :sadge:
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
All I was afraid for now was if someone could trick signalr somehow to for example log in into user who have access to "group" X, connect to websocket , then swap "group" name in query into name of a "group" he does not belong to. If not I would only check everything on connection otherwise on every method user invokes
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
So i felt as well I am mixing it too much
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Then I learned about groups in there
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
I didnt intend to use SignalR to begin with, I went into it after I felt I feel confident enough to try it
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Naming may be a little confusing, Group is my own Entity, Group can have Users - Members in it
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Sqlite D:
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
That user can invoke when he is connected
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
No no, not inside there, I mean in other functions
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
C#
public override async Task OnConnectedAsync()
{
(string groupName, string serverName, AppUser user, Group group) = await ValidateData(Context);

await Groups.AddToGroupAsync(Context.ConnectionId, groupName + "_" + serverName);

await Clients.Caller.SendAsync("NewMessage", "TestMessage");
}

private async Task<(string groupName, string serverName, AppUser user,
Group group)> ValidateData(HubCallerContext context)
{
var httpContext = context.GetHttpContext();
var groupName = httpContext?.Request.Query["groupName"].ToString();
var serverName = httpContext?.Request.Query["serverName"].ToString();
if (string.IsNullOrEmpty(groupName) || string.IsNullOrEmpty(serverName) || context.User == null)
throw new Exception("Cannot join group, did you miss group or server name?");

var user = await userRepository.GetUserByUsernameAsync(context.User.GetUsername())
?? throw new Exception("User does not exist");

var group = await groupRepository.GetGroupByGroupNameAndServerNameAsync(groupName!, serverName!)
?? throw new Exception("Group does not exist");

var groupMembers = await groupRepository.GetGroupMembersUsernamesAsync(group.Id);
if (!groupMembers.Contains(user.UserName)) throw new Exception("You are not member");

return(groupName, serverName, user, group);
}
C#
public override async Task OnConnectedAsync()
{
(string groupName, string serverName, AppUser user, Group group) = await ValidateData(Context);

await Groups.AddToGroupAsync(Context.ConnectionId, groupName + "_" + serverName);

await Clients.Caller.SendAsync("NewMessage", "TestMessage");
}

private async Task<(string groupName, string serverName, AppUser user,
Group group)> ValidateData(HubCallerContext context)
{
var httpContext = context.GetHttpContext();
var groupName = httpContext?.Request.Query["groupName"].ToString();
var serverName = httpContext?.Request.Query["serverName"].ToString();
if (string.IsNullOrEmpty(groupName) || string.IsNullOrEmpty(serverName) || context.User == null)
throw new Exception("Cannot join group, did you miss group or server name?");

var user = await userRepository.GetUserByUsernameAsync(context.User.GetUsername())
?? throw new Exception("User does not exist");

var group = await groupRepository.GetGroupByGroupNameAndServerNameAsync(groupName!, serverName!)
?? throw new Exception("Group does not exist");

var groupMembers = await groupRepository.GetGroupMembersUsernamesAsync(group.Id);
if (!groupMembers.Contains(user.UserName)) throw new Exception("You are not member");

return(groupName, serverName, user, group);
}
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.MapHub<GroupHub>("hubs/groups"); app.Run();
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
It works fine but I am learning it but if it works, it works
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
I mean in the newest version
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
Well, I do xD
133 replies
CC#
Created by Neeyo on 9/26/2024 in #help
Question about SignalR
I thought so too but its better to ask, thank you @Suiram1
133 replies