我正在开发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