总结:
我不确定是否可以将集线器放在 WCF SignalR 服务上。最好让 MVC 项目充当客户端和 Web 服务之间的代理。如果这是您的要求之一,您可以稍后使用其他客户端(例如桌面客户端)连接到 SignalR,也可以从您的 Web 服务连接到此集线器以向指定组中的客户端和/或用户发送更新。
工作流程:
首先,流程看起来更像这样:
管理客户端连接:
如果您使用内存中的方法来管理连接的用户,那么您可以首先将连接 ID 和用户 ID 添加到您用来处理此问题的任何集合中。例如:
public static ConcurrentDictionary<String, String> UsersOnline = new ConcurrentDictionary<String, String>();
public override System.Threading.Tasks.Task OnConnected()
{
UsersOnline.TryAdd(Context.ConnectionId, Context.User.Identity.GetUserId());
return base.OnConnected();
}
请注意:The Context.User will be null unless you map SignalR after the authentication.
将连接 ID 存储在客户端的变量中也可能是有益的,这样您以后可以将其传递给您的方法。
var connectionId;
var testHub = $.connection.testHub;
$.connection.hub.start().done(function () {
connectionId = $.connection.hub.id;
}
枢纽:
集线器可用于与 Web 服务进行通信。在这个例子中,我将把它用作一个肥皂服务,但其余的工作应该是一样的。
public void LongRunningTask(String ConnectionId)
{
using (var svc = new Services.MyWebService.SignalRTestServiceClient())
{
svc.LongRunningTask(ConnectionId);
} // end using
} // end LongRunningTask
请注意,我们也将连接 ID 传递给服务。当服务开始将消息发送回 MVC 项目以传递给客户端时,这就会发挥作用。
监听器或 Web API:
在 MVC 站点上设置侦听器控制器或 Web API 以接收来自 Web 服务的消息。
public ActionResult SignalR(String Message, String Type, String ConnectionId)
{
if (!String.IsNullOrWhiteSpace(Message) && !String.IsNullOrWhiteSpace(Type) && !String.IsNullOrWhiteSpace(ConnectionId))
{
if (Type == "ShowAlert")
{
// Determine if the user that started the process is still online
bool UserIsOnline = Hubs.TestHub.UsersOnline.ContainsKey(ConnectionId);
// We need this to execute our client methods
IHubContext TestHub = GlobalHost.ConnectionManager.GetHubContext<Hubs.TestHub>();
if (UserIsOnline)
{
// Show the alert to only the client that started the process.
TestHub.Clients.Client(ConnectionId).showAlert(Message);
} // end if
else
{
List<String> UserIdsInRole = new List<String>();
using (var connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
{
// Assuming you're using Identity framework since it is an MVC project, get all the ids for users in a given role.
// This is using Dapper
UserIdsInRole = connection.Query<String>(@"
SELECT ur.UserId
FROM AspNetUserRoles ur
JOIN AspNetRoles r ON ur.RoleId = r.Id
WHERE r.Name = @rolename
", new { rolename = "SpecialRole" }).ToList();
} // end using
// Find what users from that role are currently connected
List<String> ActiveUsersInRoleConnectionIds = Hubs.TestHub.UsersOnline.Where(x => UserIdsInRole.Contains(x.Value)).Select(y => y.Key).ToList();
// Send the message to the users in that role who are currently connected
TestHub.Clients.Clients(ActiveUsersInRoleConnectionIds).showAlert(Message);
} // end else (user is not online)
} // end if type show alert
} // end if nothing is null or whitespace
return new HttpStatusCodeResult(200);
} // end SignalR
网络服务:
执行长期工作的 Web 服务方法也应该接受客户端 ID,以便它可以将其发送回侦听器控制器或 Web API。它可以使用与此类似的方法(使用RestSharp)连接回MVC项目:
public void ShowAlert(String Message, String ConnectionId)
{
RestClient Client = new RestClient("http://localhost:8888");
RestRequest Request = new RestRequest("/Listener/SignalR", Method.POST);
Request.Parameters.Add(new Parameter() { Name = "Message", Type = ParameterType.QueryString, Value = Message });
Request.Parameters.Add(new Parameter() { Name = "Type", Type = ParameterType.QueryString, Value = "ShowAlert" });
Request.Parameters.Add(new Parameter() { Name = "ConnectionId", Type = ParameterType.QueryString, Value = ConnectionId });
IRestResponse Response = Client.Execute(Request);
} // end Show Alert
演示:
我做了一个概念证明并上传到Github。