问题描述
我有一个要求,以显示管理员发送通知。
我的菜单将只加载一次。
我也有广播通知页面,在这里我会发送通知给其他人。在使用集线器我将消息发送给客户节省BUTTOM的点击。
下面我未读邮件数(如我们在FB)被放置在布局中。
我的所有发送和接收code是在广播通知页面。通知计数不会得到像显示的布局菜单(如FB)上。
会有什么问题?
count是更新的网页,供管理员会发送通知。其他网页说主页或任何其他页面的通知计数这是疗法的布局不会得到更新。
答案,这将是对我很大的帮助。
集线器类:按钮点击我打电话这种方法枢纽
公共无效BroadcastNotifications(字符串消息)
{
//数据保存到数据库
Utility.AddNotification(消息);
//调用方法来获取未读邮件数(考虑状态=读/未读)
INT UnreadCount = Utility.getUnreadMessageCount();
UnreadCount = 12;
IHubContext背景= GlobalHost.ConnectionManager.GetHubContext< BroadcastMessage>();
context.Clients.All.receiveNotification(消息,UnreadCount); }
管理页面其中,通知消息将要发送。
@using(Html.BeginForm())
{
@ Html.ValidationSummary(真) <&字段集GT;
<传奇>通知和LT; /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Message)
< / DIV> &所述p为H.;
<输入类型=按钮ID =Button1的价值=创建/>
&所述; / P>
< /字段集>
} @section脚本
{
<脚本SRC =〜/脚本/ jQuery的-1.11.3.js类型=文/ JavaScript的>< / SCRIPT> <脚本SRC =〜/脚本/ jquery.signalR-1.1.4.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/ signalr /集线器>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(函数(){
$ .connection.hub.logging = TRUE;
VAR代理= $ .connection.broadcastMessage; $ .connection.hub.start()。完成(功能(){
$('#按钮1')。点击(函数(){
proxy.server.broadcastNotifications($(#消息)VAL());
});
});
proxy.client.receiveNotification =功能(消息,UnreadCount){
** $(#NOTIFICATION_COUNT)HTML(UnreadCount)。
$(#NOTIFICATION_COUNT)显示(); **
};
$ .connection.hub.start();
});
< / SCRIPT>
}
以及页面布局如果通知计数应显示为
<李班=下拉菜单ID =notification_li>
< A HREF =#类=发FA-全球发逆下拉拨动
数据帆布=体的风格=颜色:灰色;填充顶:知识+数据切换=下拉菜单
角色=按钮ARIA-haspopup =真正的咏叹调展开=false的>
&所述;跨度的id =** NOTIFICATION_COUNT **类=NOTIFICATION_COUNT大于0&下; /跨度>&下; / A>
< UL类=下拉菜单中选择ID =弹出>
< / UL>
< /李>这是_layout网页,我要显示未读mesasges的计数。
如果我触发发送按钮的未读邮件数是越来越对所有管理更新补充通知打开的网页。但是其他的网页它仍然是空的。
更新_layout按照注释
我提出了信号r客户端调用_layout
< HEAD>
<间的charset =UTF-8>
<标题>服务及LT; /标题> <! - 引导核心的CSS - >
@ Styles.Render(〜/内容/ bootstrapcss)
@ Scripts.Render(〜/包/ Modernizr的) <脚本SRC =〜/脚本/ jquery.signalR-1.1.4.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/ signalr /集线器>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(函数(){
调试器;
$ .connection.hub.logging = TRUE;
VAR代理= $ .connection.broadcastMessage; proxy.client.receiveNotification =功能(消息,UnreadCount){
调试器;
$(#NOTIFICATION_COUNT)HTML(UnreadCount);
$(#NOTIFICATION_COUNT)显示()。
}; $ .connection.hub.start(); $ .connection.hub.start()。完成(功能(){
$('#按钮1')。点击(函数(){
proxy.server.broadcastNotifications($(#消息)VAL());
});
});
});
< / SCRIPT>
< /头>
<身体GT;
@ Html.Partial(_ RightMenu)
** @ Html.Partial(_顶部菜单)** // ** Notificationcount跨度在这个局部视图**
< DIV CLASS =集装箱液体内容>
< DIV CLASS =行ID =内容> @RenderBody()< / DIV>
< BR />
< BR />
<页脚GT&;
< P>&安培;复印件; @ DateTime.Now.Year - 页脚< / P>
< /页脚>
< / DIV>
<! - 引导核心JavaScript
================================================== - >
<! - 在文档的末尾放置使页面加载速度更快 - >
@ Scripts.Render(〜/包/ jQuery的)
@ Scripts.Render(〜/包/ bootstrapjs)
@ Scripts.Render(〜/脚本/ abcjs)
@RenderSection(脚本,必需:false)
< /身体GT;
< / HTML>
我已经更新同上。在_layout有其计数我在一个局部视图显示两个部分景色。它是添加的正确途径。
t这自动生成信号r文件
$ .hubConnection.prototype.createHubProxies =功能(){
变种代理= {};
this.starting(函数(){
registerHubProxies(代理,真正的);
this._registerSubscribedHubs();
})。断开(函数(){
registerHubProxies(代理,FALSE);
});
proxies.broadcastMessage = this.createHubProxy('broadcastMessage');
proxies.broadcastMessage.client = {};
proxies.broadcastMessage.server = {
broadcastNotifications:功能(消息){
返回proxies.broadcastMessage.invoke.apply(proxies.broadcastMessage,$ .merge([BroadcastNotifications],$ .makeArray(参数)));
}
};
返回代理;
}; signalR.hub = $ .hubConnection(/ signalr,{useDefaultPath:假});
$ .extend(signalR,signalR.hub.createHubProxies());
}(window.jQuery,窗));
我认为这个问题是这样的:你只在管理员的网站收到通知
我会放在_layout内的中心脚本部分。
@ Scripts.Render(〜/包/ jQuery的)
@ Scripts.Render(〜/包/引导)<脚本SRC =〜/脚本/ jquery.signalR-2.2.0.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/ signalr /集线器>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(函数(){
调试器;
$ .connection.hub.logging = TRUE;
VAR代理= $ .connection.broadcastMessage; proxy.client.receiveNotification =功能(消息,UnreadCount){
调试器;
$(#NOTIFICATION_COUNT)HTML(UnreadCount);
$(#NOTIFICATION_COUNT)显示()。
}; $ .connection.hub.start(); $ .connection.hub.start()。完成(功能(){
$('#按钮1')。点击(函数(){
proxy.server.broadcastNotifications($(#消息)VAL());
});
});
});
< / SCRIPT>@RenderSection(脚本,必需:false)<李班=下拉菜单ID =notification_li>
< A HREF =#类=发FA-全球发逆下拉拨动
数据帆布=体的风格=颜色:灰色;填充顶:知识+数据切换=下拉菜单
角色=按钮ARIA-haspopup =真正的咏叹调展开=false的>
&所述;跨度的id =NOTIFICATION_COUNT类=NOTIFICATION_COUNT大于0&下; /跨度>
&所述; / A>
< UL类=下拉菜单中选择ID =弹出>< / UL>
< /李>
您可以在我的情况下创建了一个示例项目看:
我更新的NuGet包SignalR 2.2.0的最新版本
I have a requirement to show the notification send by administrator.
My menu will be loaded only once.
I also have broadcast notification page where I will send notification to others. On click of save buttom using Hub I am sending the message to the clients.
Here my unread message count (like we have in fb) is placed in the layout .
All my send and receive code is in the Broadcast notification page. Notification count is not getting displayed on the layout menu like (like fb) .
What will be the issue.?
Count is updated on the Webpage where admin will send notification. Other pages say Home page or any other page the notification count which is ther in the layout is not getting updated.
Answer to this will be very helpful for me.
Hub class : on button click i am calling this hub method
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);
}
Admin page where notification message is going to send .
@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>
}
And the layout page where the notification count should display is
<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.
Updated _Layout as per the comment
I have moved the signal r client call to _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>
I have updated as above. In _Layout there are two partial views in which the count i am displaying on one partial view. Is it the correct way to add.
tthis is the signal r file generated automatically
$.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));
I think the problem is the following: You are only receiving notifications on the admins' sites.
I would place the hub script section within the _layout.
@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>
You can take a look in a sample project I created for the case:
https://github.com/blfuentes/SignalR_StackOverflow_Question
I updated the nuget packages to the latest version of SignalR 2.2.0
这篇关于信号R犯规更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!