我正在尝试使用SignalR创建聊天应用程序。为了能够发送私人消息,我想将客户端分配给具有其profileID名称的组。所以我可以简单地调用组的addMessage函数发送给特定的客户端。
当我转到此页面时:https://github.com/SignalR/SignalR/wiki/Hubs
它告诉我向Hub添加一个名为Join()的函数。在这里,我可以将incomming客户端添加到组中。所以我创建了这段代码:
[HubName("Chat")]
public class ChatHub : Hub
{
public Task Join()
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
Profiel_DataHelper profiel = new Profiel_DataHelper(HttpContext.Current.User.Identity.Name);
return Groups.Add(Context.ConnectionId, profiel.ProfielID.ToString());
}
else
{
return null;
}
}
.....
当我想调用特定客户端时,我使用以下代码:
var context = GlobalHost.ConnectionManager.GetHubContext();
context.Clients.Group(profielidNaar).addTyptOnline(profielidVan);
但是当我运行程序时,根本没有调用Join()任务,因此我对该组的调用也无效。
我做错了什么?
答案 0 :(得分:1)
加入是您需要从客户端调用的集线器上的方法。没有人会为你调用它,“加入”不是一种自动调用的特殊方法。该文档向您展示了如何声明从客户端调用“可以”的方法。
还有其他方法可以了解客户端何时连接,重新连接和断开连接,详情请参见: https://github.com/SignalR/SignalR/wiki/Hubs#detecting-connect-reconnect-and-disconnect-clients-in-hubs