【问题标题】:Sending message from server to specific client is not working using SignalR 2 and MVC 4.0使用 SignalR 2 和 MVC 4.0 无法从服务器向特定客户端发送消息
【发布时间】:2017-04-08 04:38:10
【问题描述】:

我想使用 signalR 从服务器调用通知特定客户端,但它不起作用。我的代码已成功执行,但客户端没有收到来自服务器的任何调用。

但这对所有客户都有效。

var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProcessStatusNotifyHub>();
hubContext.Clients.All.notify("Got it!");

但这不适用于特定客户 [更新代码] 以下代码用chat.cshtml编写

$(function () {
       // Reference the auto-generated proxy for the hub.
       var chat = $.connection.processStatusNotifyHub;//chatHub;
       chat.client.notify = function (msg) {
            alert(msg);
       }
       // Start the connection.
       $.connection.hub.start().done(function () {
           var myClientId = $.connection.hub.id;
           console.log('connected: ' + myClientId);
           $('#sendmessageToClient').click(function () {
                //chat.server.send('imdadhusen', 'This is test text');
                $.ajax({
                        url: '@Url.Action("Send", "PushNotification")',
                        type: 'POST',
                        data: { 'clientID': myClientId },
                        dataType: 'json',
                        success: function (result) {
                            alert(result.status);
                 }
              });
           });
       });
});

以下代码是在Controller中编写的

[HttpPost]
public ActionResult Send(string clientID)
        {
             var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProcessStatusNotifyHub>();
             //hubContext.Clients.All.notify("Got it!");
             hubContext.Clients.User(clientID).notify("Got it!");

             responseResult result = new responseResult();
             result.status = "OK";
             result.message = "Notification sent successfully";
             return Json(result, JsonRequestBehavior.AllowGet);
        }

我已尝试调试它在 .cstml 或控制器上显示正确的客户端 ID 值的代码。例如clientid : 0fdf6cad-b9c1-409e-8eb7-0a57c1cfb3be

您能帮我从服务器向特定客户端发送通知吗?

【问题讨论】:

  • 你能提供完整的控制器吗?您提供的代码不清楚 connectionId 是否在您尝试执行的操作的正确范围内。
  • @Steve 我已经更新了代码供您参考,请告诉我您仍然需要更多详细信息。
  • 感谢更新。调试服务器时,clientID 是否设置为您期望的值?
  • 实际上客户端 ID 已成功传递给控制器​​,并且该语句也被执行,没有任何错误 hubContext.Clients.User(clientID).notify("Got it!"); 但客户端我没有在函数内收到消息 chat.client.notify.
  • 如果 clientID 是 SignalR 连接 ID,我想你想要 Clients.Client(clientID) 而不是 Clients.User(clientID)

标签: c# asp.net-mvc-4 signalr


【解决方案1】:

hubContext.Clients.User(string) 操作需要当前 HTTP 请求的用户 ID,而不是 SignalR 的客户端 ID。

如果您想使用客户端 ID,请使用此操作:

hubContext.Clients.Client(   )

如果你想被当前用户访问,那么你可以用这个获取当前用户ID

string UserID=User.Identity.GetUserId()

【讨论】:

    【解决方案2】:

    这不是对您特定问题的回答,而是对您的代码的建议。为什么不让客户端使用 Signalr 连接来调用服务器?只需在集线器中定义客户端可以调用的方法。然后在该方法中使用 HubCallerContext 获取 id。

    此页面向您展示如何从客户端调用服务器: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client

    然后在hub的方法中:

    Clients.User(Context.ConnectionId).notify("Got it!");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-19
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2016-05-18
      • 2021-10-10
      相关资源
      最近更新 更多