0

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();
}
  • copy-paste full exception message – vasily.sib Jul 24 '19 at 11:06
  • "'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. –  Jul 24 '19 at 11:10
  • you can set `Group` on start connection with `queryString`. – AminRostami Jul 24 '19 at 11:13

2 Answers2

0

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'););
    }
}
AminRostami
  • 2,585
  • 3
  • 29
  • 45
  • @ARF Thanks a lot for your reply. But unfortunately I get null on server side. Could you please my Update above in the question? –  Jul 24 '19 at 12:55
  • Thanks. I think there is not too many difference between Javascript and Angular side. So, how can I pass parameter in your example where you said *// do some thing here ...* ? –  Jul 24 '19 at 13:45
  • Unfortunately there is no group related part in that article :( –  Jul 24 '19 at 14:12
  • Thanks a lot for your helps, I fixed the problem using another approach. Nevertheless, voted up ;) –  Jul 25 '19 at 07:15
  • But there is a little problem mentioned on my answer, could you have a look at pls? –  Jul 25 '19 at 07:15
  • @ARF Any help pls regarding to [Cannot retrieve group in SignalR Hub](https://stackoverflow.com/questions/57196212/cannot-retrieve-group-in-signalr-hub)? –  Jul 25 '19 at 07:27
0

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://learn.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?