将用户添加到 SignalR 中的 Hub 组
Adding a user to Hub group in SignalR
我有一个基本集线器 (HubBase) class 和两个不同的集线器 class(我们称其为 HubA 和 HubB),它们继承自基本集线器 class。我将所有共享连接方法保留到 HubBase。我只想向连接到相关集线器的相应客户端发送消息。为此,我会将相关用户添加到相应的组中。例如,如果用户连接到 HubA,则应将该用户添加到 GroupA,如果连接到 HubB,则应类似地添加到 GroupB。以下是相关 classes 中的方法:
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;
}
}
问题是:我需要将组名传递给基础 class 中的 OnConnected() 方法,并在连接时将用户添加到该给定组。但是由于 "no suitable method found to override" 错误,我无法在此方法中定义 groupName 参数。我是否应该将此参数从继承的 classes 传递给基础 Class 的构造函数?或者有更聪明的方法吗?
更新: 这是我试图从客户端传递的内容:
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();
}
您可以在使用 queryString 开始连接时将 Group
名称设置为 parameter
。
并且在服务器端从 Request
.
获取
更多信息:
请在 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'););
}
}
我通过以下方法解决了这个问题:
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
}
}
但是我无法从继承的集线器 类 中获取组,如 Cannot retrieve group in SignalR Hub 中所述。有什么帮助吗?
我有一个基本集线器 (HubBase) class 和两个不同的集线器 class(我们称其为 HubA 和 HubB),它们继承自基本集线器 class。我将所有共享连接方法保留到 HubBase。我只想向连接到相关集线器的相应客户端发送消息。为此,我会将相关用户添加到相应的组中。例如,如果用户连接到 HubA,则应将该用户添加到 GroupA,如果连接到 HubB,则应类似地添加到 GroupB。以下是相关 classes 中的方法:
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;
}
}
问题是:我需要将组名传递给基础 class 中的 OnConnected() 方法,并在连接时将用户添加到该给定组。但是由于 "no suitable method found to override" 错误,我无法在此方法中定义 groupName 参数。我是否应该将此参数从继承的 classes 传递给基础 Class 的构造函数?或者有更聪明的方法吗?
更新: 这是我试图从客户端传递的内容:
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();
}
您可以在使用 queryString 开始连接时将 Group
名称设置为 parameter
。
并且在服务器端从 Request
.
更多信息:
请在 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'););
}
}
我通过以下方法解决了这个问题:
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
}
}
但是我无法从继承的集线器 类 中获取组,如 Cannot retrieve group in SignalR Hub 中所述。有什么帮助吗?