2014-02-11 4 views
2

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(); 
     } 
    } 

어떻게 작동시킬 수 있습니까?

+0

** 어떻게 ** 작동하지 않습니다? 폭발 하는가? – SLaks

+1

데이터베이스를 업데이트 할 때로드 된 웹 페이지에 대한 업데이트가 없습니다. –

+0

콘솔 및 네트워크 탭에서 무엇을 볼 수 있습니까? – SLaks

답변

1

SignalR을 사용하려면 클라이언트에 연결을 설정해야합니다. JavaScript에서는 connection.start()를 호출하여이를 수행합니다. 예를 들면 :

<!--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); 
      }); 
     }; 

     $.connection.hub.start().done(function() { 
      // You should probably be calling GetAll from somewhere. 
      // I'm not sure if you should call it as soon as you connect, 
      // but you certainly can't call GetAll before connecting. 
      alertes.server.getAll(); 
     }).fail(function (error) { 
      alert("Failed to connect!"); 
     }); 
    }); 
</script> 
당신은 여기 Signalr JS 클라이언트를 사용하는 방법에 대한 자세한 배울 수

: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client

+1

당신의 소중한 도움에 감사드립니다. 그러나 이것은 효과가 없을 것입니다 :/ –

+0

"작동하지 않을 것"에 대한 더 자세한 정보를 제공해 주시겠습니까? – halter73

+1

디버거를 참조하는 경우'스크립트 디버거가 대상 프로세스에 연결하지 못했습니다. 디버거가 이미 연결되어 있습니다. ' –

관련 문제