[英]Adding a user to Hub group in SignalR
我有一個基集線器 (HubBase) 類和兩個從基類繼承的不同集線器類(我們稱之為 HubA 和 HubB)。 我將所有共享連接方法保留到 HubBase。 我只想向連接到相關集線器的相應客戶端發送消息。 為此,我將相關用戶添加到已連接的相應組中。 例如,如果用戶連接到HubA,則該用戶應添加為GroupA,如果連接到HubB,則應將其添加為GroupB。 以下是相關類中的方法:
樞紐基地:
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);
}
}
樞紐A:
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;
}
}
樞紐B:
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();
}
您可以在使用 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'););
}
}
我通過以下方法解決了這個問題:
服務.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
}
}
但是我無法從繼承的集線器類中獲取組,如無法在 SignalR Hub 中檢索組中所述。 請問有什么幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.