2016-03-28 162 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