2016-03-28 8 views
0

Ich bin neu in SignalR und ich implementiert Signalr (asp.net MVC, SQL-Abhängigkeit) ich nur den Client mit einem bestimmten DatabaseID aktualisieren müssen (master_table.masterid)signalr Nachrichten mehrmals Ausgabe Senden

Ich bemerke Das erste Mal, wenn ich die Datensätze aktualisiere, funktioniert es gut, aber wenn ich die App ein paar Minuten lang aktiviere und dann die Datensätze aktualisiere, ruft sie mehrmals die Funktion "update-messages" an und hört dann auf zu arbeiten.

Kann jemand bitte vorschlagen, was könnte mit diesem Code falsch sein?

Dies ist der Code in meiner Haupt-Seite ist (dies ist die Index-Seite, die die anderen Layout-Seite hat) JS-Code

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

Dies ist meine Teilansicht Seite

$(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(); 
}; 

Das ist mein SQL Abhängigkeit Code

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(); 
} 
} 
} 

Das ist mein Hub Code

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

Antwort

0

Sie können überprüfen, ob sich Ihre Datenbank im Modus auto_close befindet.

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

Wenn es ist, sollten Sie es deaktivieren.

+0

Nein, ich glaube nicht, dass es aktiviert ist, da das obige sql 0 Zeilen zurückgibt. Ich denke, das Problem ist mit der Verbindung.start oder stop oder joihning und verlassen die Gruppen in der richtigen Zeit sicne die Update-Nachricht wird gefeuert mehrmals. – avatar