2016-03-20 15 views
0

私はasp.net mvc 5でアプリケーションを開発しています。このプロジェクトでは、リアルタイムで更新されたデータを表示するためにsignalRを使用しています。これは、アプリケーションUIでロードされるデータよりもデータが変更されることを意味します。しかし残念ながら、私はページを更新しない限り自動的に読み込まれません。ここでSignalRデータベース更新通知

は、以下の私のコードです:

Hub : 

[HubName("statusLog")] 
public class StatusLogHub : Hub 
{ 

    [HubMethodName("sendExportStatus")] 
    public void SendExportStatus() 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<StatusLogHub>(); 
     Clients.All.updateStatus(); 
    } 
} 

Repository : 

public class EmailStatusLogRepository 
{ 

    readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 

    public IEnumerable<EmailStatusLog> GetExportStatus() 
    { 
     var messages = new List<EmailStatusLog>(); 
     using (var connection = new SqlConnection(_connString)) 
     { 
      connection.Open(); 
      using (var command = new SqlCommand(@"SELECT * FROM dbo.EmailStatusLogs WHERE ExportStatus = 1 AND CAST(CONVERT(VARCHAR,Date,101) AS DATETIME)=CAST(CONVERT(VARCHAR,'" + DateTime.Now.Date.ToString("MM/dd/yyyy") + @"',101) AS DATETIME)", connection)) 
      { 
       command.Notification = null; 

       var dependency = new SqlDependency(command); 
       dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); 

       if (connection.State == ConnectionState.Closed) 
        connection.Open(); 

       var reader = command.ExecuteReader(); 

       while (reader.Read()) 
       { 
        messages.Add(item: new EmailStatusLog { Id = (int)reader["Id"], Investor_Code = (string)reader["Investor_Code"], EmailId = reader["EmailId"] != DBNull.Value ? (string)reader["EmailId"] : "", Date = (string)reader["Date"], ReportName = (string)reader["ReportName"], ExportStatus = (bool)reader["ExportStatus"], EmailSendStatus = (bool)reader["EmailSendStatus"], IsActive = (bool)reader["IsActive"] }); 
       } 
      } 

     } 
     return messages; 
    } 
    private void dependency_OnChange(object sender, SqlNotificationEventArgs e) 
    { 
     if (e.Type == SqlNotificationType.Change) 
     { 
      StatusLogHub statusLogHub = new StatusLogHub(); 
      statusLogHub.SendExportStatus(); 
     } 
    } 

} 

//Code from where I am updating DB: 

public void ExportStatus() 
    { 
     List<EmailStatusLog> lstEmailStatusLog = new List<EmailStatusLog>(); 
     EmailStatusLog objEmailStatusLog = new EmailStatusLog(); 
     foreach (var emailItem in lstEmailReceipent) 
     { 
      EMailDBContext _ctx = new EMailDBContext(); 
      objEmailStatusLog.EmailId = emailItem.stEmailAdd; 
      objEmailStatusLog.Investor_Code = emailItem.stInvestor_code; 
      objEmailStatusLog.Date = DateTime.Now.ToString("MM/dd/yyyy"); 
      objEmailStatusLog.ReportName = reportName; 
      objEmailStatusLog.ExportStatus = IsSuccess; 
      objEmailStatusLog.EmailSendStatus = false; 
      objEmailStatusLog.IsActive = true; 
      _ctx.emailStatusLogs.Add(objEmailStatusLog); 
      _ctx.SaveChanges(); 
      //StatusLogHub objStatusLogHub = new StatusLogHub(); 
      //objStatusLogHub.SendExportStatus(); 

     } 
    } 

Controller : 

public ActionResult GetExportStatus() 
    { 
     EmailStatusLogRepository objEmailStatusRepository = new EmailStatusLogRepository(); 
     return PartialView("_exportedReportList", objEmailStatusRepository.GetExportStatus()); 
    } 

Javascript: 

<script type="text/javascript" language="javascript"> 
//==================signalR 
$(function() { 
    var hub = $.connection.statusLog; 
    hub.client.updateStatus = function() { 
     getExportStatus() 
    }; 
    $.connection.hub.start().done(function() { 
     alert("connection started"); 
     // hub.server.sendExportStatus($('').val()); 
     getExportStatus(); 
    }).fail(function (e) { 
     alert(e); 
    }); 
}); 


function getExportStatus() { 
    var tbl = $('#statusTable'); 
    $.ajax({ 
     url: '@Url.Action("GetExportStatus")', 
     contentType: 'application/html ; charset:utf-8', 
     type: 'GET', 
     dataType: 'html' 
    }).success(function (result) { 
     tbl.empty().append(result); 
    }).error(function() { 

    }); 
} 

答えて

0

あなたのクライアントスクリプトとハブクラスのメソッドを変更する必要があります。

[HubName("statusLog")] 
public class StatusLogHub : Hub 
{ 

    [HubMethodName("sendExportStatus")] 
    public void SendExportStatus() 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<StatusLogHub>(); 
     EmailStatusLogRepository el = new EmailStatusLogRepository(); 
       Clients.All.updateStatus(el.GetExportStatus()); 
    } 
} 

とクライアント側で:

$(function() 
     { 
      var hub = $.connection.statusLog; 
      hub.client.updateStatus = function(data) { 
//data contain all the data from repository this will call every time as repository updated 
       getExportStatus(data) 
      }; 
    $.connection.hub.start().done(function() { 
       //alert("connection started"); 
       hub.server.sendExportStatus(); 
      }).fail(function(e) { 
       alert(e); 
      }); 
     }); 


function getExportStatus(data) 
     { 
alert(data); 
      //you can put here logic for getting data on html. 
    } 
} 

あなたは、クロスドメインのために、以下のリンクを参照することができます:あなたはそのハブは、すべてのクライアントにデータを送信するように、引数としてGetExportStatus方法を渡す必要がハブ方式で enter link description here

+0

おかげ@Rahulが、私はクライアント側からこのエラーを取得していますが、次のような方法で、未定義のプロパティ「sendExportStatus」を読み取ることができません:.connection.hub.startを()。 done(function(){ hub.server.sendExportStatus(); })。失敗(機能(e){ アラート(e); }); }); – moinulmithu

+0

ハブは作成されていないと思います。チェックインしてくださいコンフィグレーション –

+0

javascriptで例外があります:ハブundefind – moinulmithu

0

あなたは、クライアントスクリプトを試すことができます。

$(function() 
     { 
      var hubstatusLog = $.connection.statusLog; 
      hubstatusLog.client.updateStatus = function(data) { 
//data contain all the data from repository this will call every time as repository updated 
       getExportStatus(data) 
      }; 
    $.connection.hub.start(function() { 
hubstatusLog.server.sendExportStatus(); 
}); 
}); 

function getExportStatus(data) 
     { 
alert(data); 
      //you can put here logic for getting data on html. 
    } 

サーバーハブ:キャッチされない例外TypeError:

[HubName("statusLog")] 
public class StatusLogHub : Hub 
{ 

    [HubMethodName("sendExportStatus")] 
    public void SendExportStatus() 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<StatusLogHub>(); 
     EmailStatusLogRepository el = new EmailStatusLogRepository(); 
       Clients.All.updateStatus(el.GetExportStatus()); 
    } 
} 
+0

ありがとう@Rahul。しかし、実際には私はプロキシを生成せずにハブを使用する必要があります。これについて何か提案していただけますか? – moinulmithu

+0

したがって、リンクhttp://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#genproxyを参照することができます –

関連する問題