每次重新加载此页面时,新连接都会开始,当我更新数据库数据时通知.client.updateNotifications = function(){ 警报(" 3&#34); getAllNotifications()};
多次调用。否则它工作正常
<script src="/Scripts/jquery.signalR-2.2.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<script type="text/javascript">
var ConnectionStarted = false;
$(function () {
alert("1");
// Declare a proxy to reference the hub.
var notifications = $.connection.notificationHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateNotifications = function () {
alert("3");
getAllNotifications()
};
$.connection.hub.start().done(function () {
ConnectionStarted = true;
alert("2");
alert("connection started")
getAllNotifications();
}).fail(function (e) {
alert(e);
});
});
function getAllNotifications() {
var tbl = $('#messagesTable');
$.ajax({
url: '/Home/GetNotifications',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
</script>