私はasp.net mvcアプリケーションを開発しようとし、またsignalrを使用しようとしています。問題は、私はプロジェクトのユーザーの通知を制御する2つのテーブルがあることです。私は通知テーブルと通知とユーザーテーブルの多くのテーブルであるNotificationUserテーブルを持っています。私は、ユーザーがシステム内の別のユーザーに通知を作成すると、「Hey!New notification received」のような簡単なメッセージでユーザーを確認するポップアップを表示しようとしています。問題ストアドプロシージャSignalRの変更機能が何度も何度もヒットするのはなぜですか?
ALTER PROCEDURE [dbo].[GetNotifications]
@userid int
AS
BEGIN
select n.Ntf_Title,Ntf_Description,n.Ntf_Date from dbo.SysNotifications n INNER JOIN dbo.SysNotificationUser u on n.Ntf_ID =u.NtU_NtfID where [email protected] AND NtU_IsRead=0
END
の下にリストされている私はsignalRで使用されるように多くのtimes.Allステップを打つsignalrのjavascriptの変更機能であるハブ
[HubName("signalRHub")]
public class NtfHub : Hub
{
[HubMethodName("notifyChanges")]
public static void NotifyChanges()
{
var context = GlobalHost.ConnectionManager.GetHubContext<NtfHub>();
context.Clients.All.notifyChanges();
}
}
起動クラス
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
部分図
最終で[HttpGet]
public ActionResult GetNtf()
{
//NtfRepo rp = new NtfRepo(this.HttpContext);
string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
int userid =id;
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
using (SqlCommand sqlcom = new SqlCommand("[GetNotifications]", sqlcon))
{
sqlcon.Open();
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.AddWithValue("@userid", userid);
sqlcom.Notification = null;
SqlDependency dependancy = new SqlDependency(sqlcom);
dependancy.OnChange += dependancy_OnChange;
var reader = sqlcom.ExecuteReader();
var ntf= reader.Cast<IDataRecord>()
.Select(e => new PopulateNtfBar()
{
Title = e.GetString(0),
Description = e.GetString(1),
TimeDiff = FindDifferenceTime(e.GetDateTime(2))
}).ToList();
return PartialView("~/Views/Shared/CheckNotification.cshtml", ntf);
}
}
}
、スクリプト
ユーザーはnotification.Whereを作成した場合に非常に多くの時間を打つ$(function() {
var notification = $.connection.signalRHub;
// Create a function that the hub can call to broadcast messages.
notification.client.notifyChanges = function() {
getData();
toastr.warning("Hey,You have Ntf");
};
// Start the connection.
$.connection.hub.start().done(function() {
getData();
}).fail(function (e) {
});
});
function getData() {
var tbl = $("#header_notification_bar")
$.ajax({
url: '@Url.Action("GetNtf","Home")',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function() {
});
}
notification.client.notifyChangesが問題でしょうか?何か案が?私はそれを最適化することはできません
EDIT 1 私はコントローラでNtfHub.NotifyChangesを呼び出しています。
void dependancy_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NtfHub.NotifyChanges();
}
}
ここで、NtfHub.NotifyChanges()を呼び出していますか?おそらく、そのコードでは、サーバーメソッドがJavaScriptまたはC#から直接呼び出して複数回呼び出されている可能性があります。そのコードを追加するには[編集]できますか? – Rhumborl
@Rhumborlコードを編集しました – mayk
@Rhumborl while(true)のように打ちました – mayk