SignalR:我可以向群组中的呼叫者发送消息吗?
SignalR: Can i send a message to the caller in a group?
如果您不使用群组,您可以向来电者发送消息。
但是我希望能够在他加入该组时向客户发送消息。
这可能吗?
这是我现在使用的:
如果不使用群组则给来电者
await Clients.Caller.SendAsync("update", "You have connected to the server.");
这就是我的发现。
来自 Microsoft 网站上的“使用组”页面
发送给群组中的所有人。
Clients.Group(groupName).addChatMessage(name, message);
所有客户端除了指定的。
Clients.Group(groupName, connectionId1, connectionId2).addChatMessage(name, message);
所有客户除了来电者
Clients.OthersInGroup(groupName).addChatMessage(name, message);
您可以在 ChatHub 中添加或删除您的客户,例如
public async Task JoinGroup(string name, string group)
{
await Groups.Add(Context.ConnectionId, group);
}
public Task LeaveGroup(string name, string group)
{
return Groups.Remove(Context.ConnectionId, group);
}
并且在您的 js
端,您需要为上述两种方法添加回调,
$.connection.chatHub.server.joinGroup(connectionID, group);
$.connection.chatHub.server.leaveGroup(connectionID, group);
如果您不使用群组,您可以向来电者发送消息。 但是我希望能够在他加入该组时向客户发送消息。 这可能吗?
这是我现在使用的:
如果不使用群组则给来电者
await Clients.Caller.SendAsync("update", "You have connected to the server.");
这就是我的发现。
来自 Microsoft 网站上的“使用组”页面
发送给群组中的所有人。
Clients.Group(groupName).addChatMessage(name, message);
所有客户端除了指定的。
Clients.Group(groupName, connectionId1, connectionId2).addChatMessage(name, message);
所有客户除了来电者
Clients.OthersInGroup(groupName).addChatMessage(name, message);
您可以在 ChatHub 中添加或删除您的客户,例如
public async Task JoinGroup(string name, string group)
{
await Groups.Add(Context.ConnectionId, group);
}
public Task LeaveGroup(string name, string group)
{
return Groups.Remove(Context.ConnectionId, group);
}
并且在您的 js
端,您需要为上述两种方法添加回调,
$.connection.chatHub.server.joinGroup(connectionID, group);
$.connection.chatHub.server.leaveGroup(connectionID, group);