ASP.NET MVC 웹 응용 프로그램에 웹 소켓을 사용하려고하는데 구현할 수 없으므로 여기서 각 데이터베이스 업데이트를 최종 사용자 웹 페이지에 표시하려고합니다. 어떤 새로 고침 필요.SignalR을 사용하여 웹 소켓 구현
HTML :
<span id="nbAlertes"></span>
<ul id="listeAlertes"></ul>
자바 스크립트/SignalR/jQuery를
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<script>
$(function() {
// Declare a proxy to reference the hub.
var alertes = $.connection.AlerteHub;
// Create a function that the hub can call to broadcast messages.
alertes.client.broadcastMessage = function (nbAlertes, listeAlertes) {
// Html encode display name and message.
var nbA = $('<div />').text(nbAlertes).html();
var lstA = $('<div />').text(listeAlertes).html();
// Add the message to the page.
$('#nbAlertes').text(nbA);
lstA.forEach(function(item) {
$('#listeAlerte').append(item.nomPoste);
});
};
});
</script>
클래스 AlerteHub :
public class AlerteHub : Hub
{
public void GetAll()
{
var nbAlertes = new CalculAlertesUtilitaire().compter();
var listeAlertes = new CalculAlertesUtilitaire().lister(5);
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(nbAlertes, listeAlertes);
}
MonitoringNDataContext _db = new MonitoringNDataContext();
public string compter()
{
var compte = _db.Alertes.ToList().Count();
return (compte == 0) ? "" : compte.ToString();
}
public ICollection<AlerteModel> lister(int nb)
{
return (ICollection<AlerteModel>)_db.Alertes.ToList().Take(nb).ToArray();
}
}
클래스 시작
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
어떻게 작동시킬 수 있습니까?
** 어떻게 ** 작동하지 않습니다? 폭발 하는가? – SLaks
데이터베이스를 업데이트 할 때로드 된 웹 페이지에 대한 업데이트가 없습니다. –
콘솔 및 네트워크 탭에서 무엇을 볼 수 있습니까? – SLaks