Asp.net mvc 信号R不更新

Asp.net mvc 信号R不更新,asp.net-mvc,signalr,signalr-hub,signalr.client,Asp.net Mvc,Signalr,Signalr Hub,Signalr.client,我需要显示管理员发送的通知 我的菜单将只加载一次 我也有广播通知页面,我将发送通知给其他人。单击save按钮,使用Hub,我将向客户发送消息 在这里,我的未读邮件数(就像我们在fb中看到的一样)被放置在布局中 我所有的发送和接收代码都在广播通知页面中。通知计数未显示在布局菜单上,如(如fb) 问题是什么 计数在管理员将发送通知的网页上更新。其他页面表示主页或任何其他页面布局中的通知计数未得到更新 这个问题的答案对我很有帮助 集线器类:单击按钮,我调用此集线器方法 public void

我需要显示管理员发送的通知

我的菜单将只加载一次

我也有广播通知页面,我将发送通知给其他人。单击save按钮,使用Hub,我将向客户发送消息

在这里,我的未读邮件数(就像我们在fb中看到的一样)被放置在布局中

我所有的发送和接收代码都在广播通知页面中。通知计数未显示在布局菜单上,如(如fb)

问题是什么

计数在管理员将发送通知的网页上更新。其他页面表示主页或任何其他页面布局中的通知计数未得到更新

这个问题的答案对我很有帮助

集线器类:单击按钮,我调用此集线器方法

    public void BroadcastNotifications(string message)
    {
        // Save data to database 
        Utility.AddNotification(message);
        // Call method to get the number of unread messages (consider the status = read / unread)
        int UnreadCount = Utility.getUnreadMessageCount();
        UnreadCount = 12;
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastMessage>();
        context.Clients.All.receiveNotification(message, UnreadCount);

    }

我认为问题在于:您只在管理员的站点上收到通知

我会将hub脚本部分放在_布局中

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

<script src="~/Scripts/jquery.signalR-2.2.0.js" type="text/javascript"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
    $(function () {
        debugger;
        $.connection.hub.logging = true;
        var proxy = $.connection.broadcastMessage;

        proxy.client.receiveNotification = function (message, UnreadCount) {
            debugger;
            $("#notification_count").html(UnreadCount);
            $("#notification_count").show();
        };

        $.connection.hub.start();

        $.connection.hub.start().done(function () {
            $('#button1').click(function () {
                proxy.server.broadcastNotifications($("#Message").val());
            });
        });
    });
</script>

@RenderSection("scripts", required: false)

<li class="dropdown" id="notification_li">
    <a href="#" class="fa fa-globe fa-inverse dropdown-toggle"
       data-canvas="body" style="color:gray;padding-top:17px" data-toggle="dropdown"
       role="button" aria-haspopup="true" aria-expanded="false">
        <span id="notification_count" class="notification_count">0</span>
    </a>
    <ul class="dropdown-menu" id="popup"></ul>
</li>
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
$(函数(){
调试器;
$.connection.hub.logging=true;
var proxy=$.connection.broadcastMessage;
proxy.client.receiveNotification=函数(消息,未登录){
调试器;
$(“#通知计数”).html(未计数);
$(“通知计数”).show();
};
$.connection.hub.start();
$.connection.hub.start().done(函数(){
$('#按钮1')。单击(函数(){
proxy.server.broadcastNotifications($(“#Message”).val());
});
});
});
@RenderSection(“脚本”,必需:false)
    • 您可以查看我为该案例创建的示例项目:


      我将nuget软件包更新为最新版本的SignalR 2.2.0

      我认为问题在于:您只在管理员网站上收到通知

      我会将hub脚本部分放在_布局中

      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/bootstrap")
      
      <script src="~/Scripts/jquery.signalR-2.2.0.js" type="text/javascript"></script>
      <script src="~/signalr/hubs"></script>
      <script type="text/javascript">
          $(function () {
              debugger;
              $.connection.hub.logging = true;
              var proxy = $.connection.broadcastMessage;
      
              proxy.client.receiveNotification = function (message, UnreadCount) {
                  debugger;
                  $("#notification_count").html(UnreadCount);
                  $("#notification_count").show();
              };
      
              $.connection.hub.start();
      
              $.connection.hub.start().done(function () {
                  $('#button1').click(function () {
                      proxy.server.broadcastNotifications($("#Message").val());
                  });
              });
          });
      </script>
      
      @RenderSection("scripts", required: false)
      
      <li class="dropdown" id="notification_li">
          <a href="#" class="fa fa-globe fa-inverse dropdown-toggle"
             data-canvas="body" style="color:gray;padding-top:17px" data-toggle="dropdown"
             role="button" aria-haspopup="true" aria-expanded="false">
              <span id="notification_count" class="notification_count">0</span>
          </a>
          <ul class="dropdown-menu" id="popup"></ul>
      </li>
      
      @Scripts.Render(“~/bundles/jquery”)
      @Scripts.Render(“~/bundles/bootstrap”)
      $(函数(){
      调试器;
      $.connection.hub.logging=true;
      var proxy=$.connection.broadcastMessage;
      proxy.client.receiveNotification=函数(消息,未登录){
      调试器;
      $(“#通知计数”).html(未计数);
      $(“通知计数”).show();
      };
      $.connection.hub.start();
      $.connection.hub.start().done(函数(){
      $('#按钮1')。单击(函数(){
      proxy.server.broadcastNotifications($(“#Message”).val());
      });
      });
      });
      @RenderSection(“脚本”,必需:false)
      
      • 您可以查看我为该案例创建的示例项目:


        我将nuget软件包更新为最新版本的SignalR 2.2.0

        谢谢您的编辑。您能显示当前代码吗?用代码编辑了我的问题谢谢您的编辑。您能显示当前代码吗?用代码编辑了我的问题我已经按照您在问题部分中的建议编辑了代码。在布局中,我有两个局部视图,其中我将在一个局部视图中显示计数。但现在我也错过了通知。我使用的是VS2010,它不接受signal R2.2.0版本。@jubi signal加载应该放在jquery之后。试试看。您还可以在浏览器中打开一个inspect窗口,查看javascript代码中的错误。在jquery之后。就像你回复中的代码一样。但还是一样的行为。如果我查看页面,源jquery在signal r reference之前。如何打开inspect窗口。你是说firefox中的firebug吗?检查网站是通过Chrome:ctrl+shift+I或右键单击检查元素来完成的,firefox:right click->Inspect元素。设置一些
        调试器加载时要停止的位置。@jubi信号器加载应放在jquery之后。试试看。您还可以在浏览器中打开inspect窗口,查看javascript代码中的错误我想把这个标记为答案。它解决了问题。谢谢blacai。谢谢你回答我在StackOverflow中的第一个问题。我已经按照你在问题部分中的建议编辑了代码。在布局中,我有两个局部视图,其中我将在一个局部视图中显示计数。但现在我也错过了通知。我使用的是VS2010,它不接受signal R2.2.0版本。@jubi signal加载应该放在jquery之后。试试看。您还可以在浏览器中打开一个inspect窗口,查看javascript代码中的错误。在jquery之后。就像你回复中的代码一样。但还是一样的行为。如果我查看页面,源jquery在signal r reference之前。如何打开inspect窗口。你是说firefox中的firebug吗?检查网站是通过Chrome:ctrl+shift+I或右键单击检查元素来完成的,firefox:right click->Inspect元素。设置一些
        调试器加载时要停止的位置。@jubi信号器加载应放在jquery之后。试试看。您还可以在浏览器中打开inspect窗口,查看javascript代码中的错误我想把这个标记为答案。它解决了问题。谢谢blacai。谢谢你回答我在stackoverflow的第一个问题
        
          <head>
            <meta charset="utf-8">
            <title>Services</title>
        
            <!-- Bootstrap core CSS -->
            @Styles.Render("~/Content/bootstrapcss")
            @Scripts.Render("~/bundles/modernizr")
        
            <script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
            <script src="~/signalr/hubs"></script>
            <script type="text/javascript">
            $(function () {
                debugger;
                $.connection.hub.logging = true;
                var proxy = $.connection.broadcastMessage;
        
                proxy.client.receiveNotification = function (message, UnreadCount) {
                    debugger;
                    $("#notification_count").html(UnreadCount);
                    $("#notification_count").show();
                };
        
                $.connection.hub.start();
        
                $.connection.hub.start().done(function () {
                    $('#button1').click(function () {
                        proxy.server.broadcastNotifications($("#Message").val());
                    });
                });
            });
        </script>
        </head>
            <body>
            @Html.Partial("_RightMenu")
            **@Html.Partial("_TopMenu")  **//** Notificationcount span is in this partial view**
            <div class="container-fluid body-content">
                <div class="row" id="Content">@RenderBody()</div>
                <br />
                <br />
                <footer>
                    <p>&copy; @DateTime.Now.Year - FOOTER</p>
                </footer>
            </div>
            <!-- Bootstrap core JavaScript
            ================================================== -->
            <!-- Placed at the end of the document so the pages load faster -->
            @Scripts.Render("~/bundles/jquery")
            @Scripts.Render("~/bundles/bootstrapjs")
            @Scripts.Render("~/Scripts/abcjs")
            @RenderSection("scripts", required: false)
          </body>
        </html>
        
            $.hubConnection.prototype.createHubProxies = function () {
                var proxies = {};
                this.starting(function () {
                    registerHubProxies(proxies, true);
                    this._registerSubscribedHubs();
                }).disconnected(function () {
                    registerHubProxies(proxies, false);
                });
                proxies.broadcastMessage = this.createHubProxy('broadcastMessage'); 
                proxies.broadcastMessage.client = { };
                proxies.broadcastMessage.server = {
                broadcastNotifications: function (message) {
                    return proxies.broadcastMessage.invoke.apply(proxies.broadcastMessage, $.merge(["BroadcastNotifications"], $.makeArray(arguments)));
                }
            };
                return proxies;
            };
        
            signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
            $.extend(signalR, signalR.hub.createHubProxies());
        }(window.jQuery, window));
        
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        
        <script src="~/Scripts/jquery.signalR-2.2.0.js" type="text/javascript"></script>
        <script src="~/signalr/hubs"></script>
        <script type="text/javascript">
            $(function () {
                debugger;
                $.connection.hub.logging = true;
                var proxy = $.connection.broadcastMessage;
        
                proxy.client.receiveNotification = function (message, UnreadCount) {
                    debugger;
                    $("#notification_count").html(UnreadCount);
                    $("#notification_count").show();
                };
        
                $.connection.hub.start();
        
                $.connection.hub.start().done(function () {
                    $('#button1').click(function () {
                        proxy.server.broadcastNotifications($("#Message").val());
                    });
                });
            });
        </script>
        
        @RenderSection("scripts", required: false)
        
        <li class="dropdown" id="notification_li">
            <a href="#" class="fa fa-globe fa-inverse dropdown-toggle"
               data-canvas="body" style="color:gray;padding-top:17px" data-toggle="dropdown"
               role="button" aria-haspopup="true" aria-expanded="false">
                <span id="notification_count" class="notification_count">0</span>
            </a>
            <ul class="dropdown-menu" id="popup"></ul>
        </li>