3
我使用SignalR 3 RC1与ASP.NET 5为我的项目,我有麻烦我的客户预订了特定小组,从我的服务器接收消息。
HUB类
[HubName("ChatHub")]
public class ChatHub : Hub
{
public async Task Join(string userId)
{
if (!string.IsNullOrEmpty(userId))
{
await Groups.Add(Context.ConnectionId, userId);
}
}
}
JS代码
var chatHub = hubConnection.createHubProxy("chatHub");
chatHub .on("newMessage", (response) => {
console.log(response);
});
hubConnection.start().done(response => {
chatHub.invoke("join", "userid");
});
的WebAPI
public class ChatController : ApiController
{
protected readonly IHubContext ChatHub;
public ChatController(IConnectionManager signalRConnectionManager)
{
ChatHub = signalRConnectionManager.GetHubContext<ChatHub>();
}
[Authorize]
[HttpPost]
[Route("Message")]
public async Task<IActionResult> CreateMessage([FromBody] messageParams dto)
{
await ChatHub.Clients.Group("userid").NewMessage("hello world");
}
}
如果我播出的 “所有” 客户端它正在工作。
await ChatHub.Clients.All.NewMessage("hello world");
是否有将消息广播到特定组的特定配置?
我降级到signalR 2.2和它的作品。 –