0
votes

I have a base hub (HubBase) class and two different hub classes (let's call HubA and HubB) that inherited from base class. I keep all the shared connection methods to HubBase. I want to send message only the corresponding clients connected to the related hub. For this reason I will add the related user to the corresponding group on connected. For example if a user connects to HubA, this user should be added GroupA, and if connects to HubB, should be added GroupB similarly. Here are the methods in the related classes below:

HubBase:

public class HubBase : Hub
{
    public readonly static ConnectionMapping<string> _connections =
        new ConnectionMapping<string>();

    public override async Task OnConnected()
    {
        /* !!! Here I need the user to the given group name. But I cannot define groupName 
        parameter in this method due to "no suitable method found to override" error */
        await Groups.Add(Context.ConnectionId, "groupA");

        string name = Context.User.Identity.Name;
        _connections.Add(name, Context.ConnectionId);
        await base.OnConnected();
    }

    public override async Task OnDisconnected(bool stopCalled)
    {
        await Groups.Remove(Context.ConnectionId, "groupA");

        string name = Context.User.Identity.Name;
        _connections.Remove(name, Context.ConnectionId);
        await base.OnDisconnected(stopCalled);
    }
}


HubA:

public class HubA : HubBase
{
    private static IHubContext context = GlobalHost.ConnectionManager.GetHubContext<HubA>();

    public async Task SendMessage(string message)
    {
        await context.Clients.Group("groupA", message).sendMessage;
    }
}


HubB:

public class HubB : HubBase
{
    private static IHubContext context = GlobalHost.ConnectionManager.GetHubContext<HubB>();

    public async Task SendMessage(string message)
    {
        await context.Clients.Group("groupB", message).sendMessage;
    }
}

The problem is that: I need to pass group name to the OnConnected() method in base class and add the user to this given group on connection. But I cannot define groupName parameter in this method due to "no suitable method found to override" error. Should I pass this parameter to the base Class's constructor from inherited classes? Or is there a smarter way?

Update: Here is what I tried to pass from client side:

HubService.ts:

export class HubService {
    private baseUrl: string;
    private proxy: any;
    private proxyName: string = 'myHub';
    private connection: any;         

    constructor(public app: AppService) {
        this.baseUrl = app.getBaseUrl();
        this.createConnection();
        this.registerOnServerEvents();
        this.startConnection();
    }

    private createConnection() {
        // create hub connection
        this.connection = $.hubConnection(this.baseUrl);

        // create new proxy as name already given in top  
        this.proxy = this.connection.createHubProxy(this.proxyName);
    }

    private startConnection(): any {
        this.connection
            .start()
            .done((data: any) => {
                this.connection.qs = { 'group': 'GroupA' };
            })
    }
}

HubBase.cs:

public override async Task OnConnected()
{
    var group = Context.QueryString["group"]; // ! this returns null
    await Groups.Add(Context.ConnectionId, group);

    string name = Context.User.Identity.Name;
    _connections.Add(name, Context.ConnectionId);
    await base.OnConnected();
}
2
copy-paste full exception messagevasily.sib
"'HubBase.OnConnected(string)': no suitable method found to override Xxx\HubBase.cs.". But I rather than error message, I am wondering how to add a user from HubA, HubB by giving a group name. On the ther hand, there may be another solution on client side by giving the group name and passing to the HubA, HubB.user5871859
you can set Group on start connection with queryString.AminRostami

2 Answers

0
votes

you can set Group name as parameter on start connection with queryString.

and in server side get from Request.

more info:

SignalR Client How to Set user when start connection?

please try this code in ts:

export class HubService 
{
    hubConnection: HubConnection;
    constructor(public app: AppService) { this.startConnection();}

    private startConnection(): any 
    {
        let builder = new HubConnectionBuilder();
        this.hubConnection = builder.withUrl('http://localhost:12345/HubBase?group=GroupA').build();
        this.hubConnection.start().catch(() => console.log('error'););
    }
}
0
votes

I fixed the problem via the following approach:

service.ts:

private startConnection(): any {

    //pass parameter viw query string
    this.connection.qs = { 'group': 'myGroup' }; 

    this.connection
        .start()
        .done((data: any) => {
            //
        })
}

HubBase.cs:

public class HubBase : Hub
{
    public readonly static ConnectionMapping<string> _connections =
        new ConnectionMapping<string>();

    public override async Task OnConnected()
    {
        var group = Context.QueryString["group"];

        //I tried to add the user to the given group using one of the following method
        await Groups.Add(Context.ConnectionId, group); 
        await AddToGroup(Context.ConnectionId, group);

        //other stuff
    }
}

https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#how-to-configure-the-connection

But I cannot get the group from the inherited hub classes as explained on Cannot retrieve group in SignalR Hub. Any help please?