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"]);
}
いいえ、上記のsqlが0の行を返したために有効になっているとは思いません。問題はconnection.startであると思うか、停止しているか、またはrihグループを適切な時間に残してください。数回。 – avatar