我开发了一个需要使用 signalR 的 asp.net mvc 5 应用程序。我已经创建了集线器,并且能够与集线器连接。现在我面临的主要问题是如何将服务器端方法与集线器映射。如果有任何可以帮助我的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
{
EMailDBContext _ctx = new EMailDBContext();
//I need to show this lstEmailStatus list in real time.
public IEnumerable<EmailStatusLog> GetExportStatus()
{
var lstEmailStatus = _ctx.emailStatusLogs.Where(x => x.IsActive == true && x.Date == DateTime.Now.ToString()).ToList();
return lstEmailStatus;
}
}
Controller :
public ActionResult GetExportStatus()
{
EmailStatusLogRepository objEmailStatusRepository = new EmailStatusLogRepository();
return PartialView("_exportedReportList", objEmailStatusRepository.GetExportStatus());
}
View:
$("#btnExportStatus").click(function () {
$.ajax({
url: '@Url.Action("GetExportStatus")',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function () {
var connection = $.hubConnection();
var hub = connection.createHubProxy("statusLog");
hub.on("updateStatus", function (statusUpdated) {
$('#hitCountValue').text(statusUpdated);
});
});
// Declare a proxy to reference the hub.
});
function getExportStatus() {
var tbl = $('#statusTable');
$(function (result) {
tbl.empty().append(result);
}).error(function () {
});
};