这就是我的做法,有点hack,但它有效。它仅在从控制器调用集线器时有效,否则将不起作用。使用 UpdateHubFromController 方法。
public class UpdateFeedActivity : Hub
{
readonly IHubContext _hubContext ;
public UpdateFeedActivity()
{
_hubContext = _hubContext ?? GlobalHost.ConnectionManager.GetHubContext<UpdateFeedActivity>();
}
public void UpdateFeed(string groupname, string itemId, Enums.FeedActivityTypes activityType,
string userId="",
string actionResult1="",
string actionResult2="",
string actionResult3="")
{
Clients.Group(groupname).updateMessages(new FeedHubResponse
{
ItemId = itemId,
ActivityType = activityType.ToString(),
UserId = userId,
ActionResult1 = actionResult1,
ActionResult2 = actionResult2,
ActionResult3 = actionResult3
});
}
public void UpdateFeedFromController(string groupname, string itemId, Enums.FeedActivityTypes activityType,
string userId = "",
string actionResult1 = "",
string actionResult2 = "",
string actionResult3 = "")
{
_hubContext.Clients.Group(groupname).updateMessages(new FeedHubResponse
{
ItemId = itemId,
ActivityType = activityType.ToString(),
UserId = userId,
ActionResult1 = actionResult1,
ActionResult2 = actionResult2,
ActionResult3 = actionResult3
});
}
public void Join(string groupname)
{
Groups.Add(Context.ConnectionId, groupname);
}
}
public class FeedHubResponse
{
public string ItemId { get; set; }
public string UserId { get; set; }
public string ActionResult1 { get; set; }
public string ActionResult2 { get; set; }
public string ActionResult3 { get; set; }
public string ActivityType { get; set; }
public string Text { get; set; }
}