C# 信号r通知图标铃mvc 5
我是MVC的新手。我看到了许多与显示实时通知的C# 信号r通知图标铃mvc 5,c#,jquery,asp.net-mvc,signalr,C#,Jquery,Asp.net Mvc,Signalr,我是MVC的新手。我看到了许多与显示实时通知的signal相关的教程,但这些教程不能满足我的要求。我在这个youtube链接上写了同样的代码——仍然没有发生任何事情 我想要的是,当我在数据库中插入新项目时,它应该在glyphicon bell上发出通知,并弹出插入的数据。我在Startup中添加了Owinclass,NotificationHubclass也在Global.asax中编写了代码 在布局页面上 <li> <span class="noti glyphic
signal
相关的教程,但这些教程不能满足我的要求。我在这个youtube链接上写了同样的代码——仍然没有发生任何事情
我想要的是,当我在数据库中插入新项目时,它应该在glyphicon bell上发出通知,并弹出插入的数据。我在Startup中添加了Owin
class,NotificationHub
class也在Global.asax中编写了代码
在布局页面上
<li>
<span class="noti glyphicon glyphicon-bell"><span class="count"> </span>
</span>
<div class="noti-content">
<div class="noti-top-arrow">
</div>
<ul id="noticontent"></ul>
</div>
</li>
<script src="~/scripts/jquery-3.1.1.min.js"></script>
<script src="~/scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="/signalr/hubs"></script>
<script src="~/scripts/bootstrap.min.js"></script>
<style type="text/css">
.noti-content {
position: fixed;
right: 100px;
background: #ff0000;
border-radius: 4px;
top: 47px;
width: 250px;
display: none;
border: 1px solid azure;
}
ul#noticontent {
max-height: 200px;
overflow: auto;
padding: 0px;
margin: 0px;
padding-left: 20px;
}
ul#noticontent li {
margin: 3px;
padding: 6px;
background: #ff0000;
}
.noti-top-arrow {
border-color: transparent;
border-bottom-color: black;
border-style: dashed dashed solid;
border-width: 0 8.5px 8.5px;
position: absolute;
right: 32px;
top: -8px;
}
span.noti {
color: #2A3F54;
margin: 15px;
position: fixed;
right: 100px;
font-size: 18px;
cursor: pointer;
}
span.count {
position: relative;
top: -3px;
}
</style>
在日志上,其唯一显示信号r已启动。程序运行,但在插入项目时不执行任何操作 在
启动连接之前,您需要注册客户端方法
函数
//signal r js code to start hub and send recieve notification
//signalr method for push server message to client
notificationHub.client.notify = function (message) {
if (message && message.toLowerCase() == "added") {
updateNotificationCount();
}
}
var notificationHub = $.connection.notificationHub;
$.connection.hub.start().done(function () {
console.log('notification hub started');
});
您需要调用该方法
public void Hello()
{
--Your Code--
}
您可以通过在hub.start()函数中进行服务器调用来实现
与从Hub
连接到client
的方式相同,如下所示
notificationHub.Clients.All.notify("added");
你需要更新这个
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var currentdate = d.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day;
var notificationHub = $.connection.notificationHub;
$.connection.hub.start().done(function () {
console.log('notification hub started');
notificationHub.server.RegisterNotification(currentdate);
});
//for calling hello() method you can do the same thing inside the above function notificationHub.server.Hello();
var d=新日期();
变量月份=d.getMonth()+1;
var day=d.getDate();
var currentdate=d.getFullYear()+'/'+(''+月).lengthnotificationHub.client.notify
未执行,因为没有调用hub服务器函数的事件,因此该函数未从hub服务器调用。我创建了一个notificationHub类:hub为什么未执行。请您指导我在哪里编辑代码。谢谢。您有hubClass,没关系。But我没有看到任何调用中心服务器函数的代码,比如notificationHub.server.yourFunctionName()
我编辑过notificationHub,比如public class notificationHub:hub{public void Hello(){this.Clients.All.Hello(“added”);}
但这不起作用。
public void Hello()
{
--Your Code--
}
notificationHub.Clients.All.notify("added");
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var currentdate = d.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day;
var notificationHub = $.connection.notificationHub;
$.connection.hub.start().done(function () {
console.log('notification hub started');
notificationHub.server.RegisterNotification(currentdate);
});
//for calling hello() method you can do the same thing inside the above function notificationHub.server.Hello();