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();
}
Group
on start connection withqueryString
. – AminRostami