我有要求顯示管理員發送的通知。Signal R不更新
我的菜單隻會被加載一次。
我也有廣播通知頁面,在這裏我會發送通知給其他人。點擊使用Hub保存按鈕我正在向客戶端發送消息。
這裏我未讀郵件數(如我們在FB)被放置在佈局。
我所有的發送和接收代碼在廣播通知頁面。通知計數未顯示在佈局菜單上(如fb)。
會是什麼問題。
計數更新網頁上其中admin會發送通知。其他頁面說主頁或任何其他頁面佈局中的通知計數沒有得到更新。
對此的回答對我很有幫助。
HUB類:按鈕點擊我打電話這個樞紐方法
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);
}
管理頁面發送通知消息要發送。
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Notification</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<p>
<input type="button" id="button1" value="Create" />
</p>
</fieldset>
}
@section Scripts
{
<script src="~/Scripts/jquery-1.11.3.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
$(function() {
$.connection.hub.logging = true;
var proxy = $.connection.broadcastMessage;
$.connection.hub.start().done(function() {
$('#button1').click(function() {
proxy.server.broadcastNotifications($("#Message").val());
});
});
proxy.client.receiveNotification = function (message, UnreadCount) {
**$("#notification_count").html(UnreadCount);
$("#notification_count").show();**
};
$.connection.hub.start();
});
</script>
}
而且佈局頁面該通知計數應顯示爲
<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>
This is the _layout page where i have to display the count of unread mesasges.
If i trigger the send button the unread count is getting updated on all the admin add notification open pages. But the other pages it remains empty.
更新_layout按照註釋
我提出信號r客戶端調用_layout
<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>© @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>
我已經更新如上。在_Layout中有兩個部分視圖,其中我在一個局部視圖上顯示計數。這是添加的正確方法嗎?
t這我認爲這個問題是下面自動生成
$.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));
感謝您的編輯.. – jubi
您能否顯示當前的代碼? – blfuentes
用代碼編輯我的問題 – jubi