【发布时间】:2019-12-02 12:17:48
【问题描述】:
我有一个基集线器 (HubBase) 类和两个从基类继承的不同集线器类(我们称之为 HubA 和 HubB)。我将所有共享连接方法保留到 HubBase。我只想向连接到相关集线器的相应客户端发送消息。出于这个原因,我会将相关用户添加到已连接的相应组中。例如,如果用户连接到 HubA,则应将此用户添加到 GroupA,如果连接到 HubB,则应添加 GroupB。下面是相关类中的方法:
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;
}
}
问题是:我需要将组名传递给基类中的 OnConnected() 方法,并在连接时将用户添加到这个给定的组。但是由于“找不到合适的方法来覆盖”错误,我无法在此方法中定义 groupName 参数。我应该将此参数从继承的类传递给基类的构造函数吗?还是有更聪明的方法?
更新:这是我试图从客户端传递的内容:
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();
}
【问题讨论】:
-
复制粘贴完整异常信息
-
“'HubBase.OnConnected(string)': 找不到合适的方法来覆盖 Xxx\HubBase.cs。”。但我不是错误消息,而是想知道如何通过提供组名从 HubA、HubB 添加用户。另一方面,在客户端可能有另一种解决方案,方法是给出组名并传递给 HubA,HubB。
-
您可以设置
Group与queryString开始连接。
标签: c# asp.net asp.net-mvc asp.net-core signalr