2016-03-28 123 views
0

我是新来SignalR和我实现Signalr(asp.net MVC,SQL依赖) 我需要一定的databaseID只更新客户端(master_table.masterid)signalr发送消息几次问题

通知我前几次更新记录时,它工作正常,但如果我将应用程序保留几分钟,然后更新记录,它会不断调用“更新消息”功能多次,然后停止工作。

任何人都可以请建议什么可能是错误的这段代码?

这是在我的主页的代码(这是索引页具有与另一布局页)

<div style="overflow:auto;" class="panel-body"> 
@Html.Action("SignalRTesterPartialView", "MasterTester") 
</div> 

这是我的局部视图页面JS代码

$(function() { 
    var dialog, form 
    // Declare a proxy to reference the hub. 

    var notifications = $.connection.messagesHub; 
    //debugger; 
    //Create a function that the hub can call to broadcast messages. 
    notifications.client.updateMessages = function (hName) { 
    alert(hName + "in update message"); 
    getoneMessages(hName) 
    notifications.server.leaveGroup(hName); 
    }; 
    // Start the connection. 

    $.connection.hub.qs = { 'System_Name': '2' } 
    $.connection.hub.logging = true; 
    $.connection.hub.start().done(function() { 
    var hostName =getUrlVars()["System_Name"]; 
    //alert('connected'); 
    notifications.server.joinGroup(hostName); 
    }).fail(function (e) { 
    alert(e); 
    }); 
    }); 

    function getUrlVars() { 
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for (var i = 0; i < hashes.length; i++) { 
    hash = hashes[i].split('='); 
    vars.push(hash[0]); 
    vars[hash[0]] = hash[1]; 
    } 
    return vars; 
    } 

function getoneMessages(hName) { 
    var tbl = $('#selectable'); 
    //alert('mesgID=' + mesgID) 
    //var tbl = $('#selectable'); 
    $.ajax({ 
    url: '/MasterTester/SignalRTesterPartialView', 
    cache: false, 
    contentType: 'application/html ; charset:utf-8', 
    type: 'GET', 
    dataType: 'html' 
    }).success(function (result) { 
    //alert(result); 
    tbl.empty().append(result); 
    }).error(function (exception) { 
    //alert('failed= ' + exception); 
    }); 
    } 

window.onbeforeunload = function (e) { 
$.connection.hub.stop(); 
}; 

这是我SQL依赖代码

public PartialViewResult TesterView() 
{ 
commandText = "select various fields where MasterKeyId=" + masterID;" 
using (SqlConnection connection = new SqlConnection(regularConnectionString)) 
{ 
using (SqlCommand command = new SqlCommand(commandText, connection)) 
{ 
connection.Open(); 

var dependency = new SqlDependency(command); 
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); 

// NOTE: You have to execute the command, or the notification will never fire. 
var reader = command.ExecuteReader(); 
} 
} 
} 

这是我的枢纽代码

public static void SendMessages(string hName) 
{ 
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>(); 
hostName = hName; 
context.Clients.Group(hostName).updateMessages(hName); 
} 

public Task leaveGroup(string hName) 
{ 
return Groups.Remove(Context.ConnectionId, hName); 
} 

public Task joinGroup(string hName) 
{ 
return Groups.Add(Context.ConnectionId, hName); 
} 

public Task OnDisconnected(IRequest request, string mID) 
{ 
return Groups.Remove(Context.ConnectionId, request.QueryString["System_Name"]); 
} 

回答

0

您可以检查您的数据库是否处于auto_close模式。

select name from master.sys.databases where is_auto_close_on = 1 

如果是你应该禁用它。

+0

不,我不认为它启用,因为上面的sql返回0行。我认为问题是与connection.start或停止或joihning并离开rthe组在正确的时间sicne更新消息正在被解雇几次。 – avatar