发信号码多次发送信号

时间:2016-03-28 18:55:41

标签: asp.net-mvc-4 signalr signalr-hub sqldependency

我是SignalR的新手,我实现了Signalr(asp.net MVC,sql依赖) 我只需要使用某个databaseID(master_table.masterid)

更新客户端

我注意到,最初几次更新记录,它工作正常,但如果我让应用程序开启几分钟然后更新记录,它会继续调用" update-messages"多次运行然后停止工作。

有人可以建议这段代码有什么问题吗?

这是我的主页面中的代码(这是具有另一个布局页面的索引页面)

<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"]);
}

1 个答案:

答案 0 :(得分:0)

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

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

如果是,你应该禁用它。