我正在開發WPF應用程序,其中程序的一個實例將能夠通過SignalR Self-Host與其他人通信。一切都很好,除了我不知道如何以及在哪裏加入一個組,以便程序知道哪些用戶發送通知。任何幫助,將不勝感激。 這裏是我的示例代碼:在SignalR中加入組
//client side
private async void ConnectAsync()
{
Connection = new HubConnection(ServerURI);
Connection.Closed += Connection_Closed;
HubProxy = Connection.CreateHubProxy("MyHub");
//Handle incoming event from server: use Invoke to write to console from SignalR's thread
HubProxy.On<string, string>("AddMessage", (name, message) =>
this.Dispatcher.Invoke(() => RichTextBoxConsole.AppendText(String.Format("{0}: {1}\r", name, message))
)
);
try
{
await Connection.Start();
}
catch (HttpRequestException)
{
StatusText.Content = "Unable to connect to server: Start server before connecting clients.";
//No connection: Don't enable Send button or show chat UI
return;
}
HubProxy.Invoke<string>("JoinGroup", "foobar").Wait(); // Do I have to do it here?
}
//serverside
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.Group("foobar").AddMessage(name, message);
}
public Task JoinGroup(string groupName)
{
return Groups.Add(Context.ConnectionId, groupName);
}
public Task AddGroups()
{
return Groups.Add(Context.ConnectionId, "foobar");
}
public override Task OnConnected()
{
return AddGroups();
}
}
謝謝你的幫助,但事實並非如此。我已發佈解決方案,解決了這個問題。 – niks