signalR Uncaught TypeError: Cannot read property ''etathub"of undefined
bonjour tous,
Je rencontre un souci sur lequel je bute depuis un moment.
le but de mon code et d’afficher et faire mise a jour a mon page index.html a chaque modification dans la base de donner ,
pour faire cela je me suis baser sur SqlDependency ,signalR.
voici mon class qui Projet dans la base
-
Code:
1 2 3 4 5 6 7 8 9 10 11
| public class vitesseinstantaner
{
[Key]
[Column(Order = 0)]
public string Idmachine { get; set; }
[Key]
[Column(Order = 1)]
public DateTime time { get; set; }
public float vitesse { get; set; }
} |
et encore un autre Class je vais l'utiliser pour envoyer cette objet comme notification
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| public class Etatmachine
{
public string id_machine { get; set; }
public float vi { get; set; }
public List<float> lastVis { get; set; }
public Etatmachine(string id, float vi, List<float> lastVis)
{
this.id_machine = id;
this.lastVis = lastVis;
this.vi = vi;
}
} |
et voici mon class Etathub
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| public class EtatHub: Hub {
private bddContext db = new bddContext();
public static void Show(Etatmachine etat)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<EtatHub>();
context.Clients.All.RecieveNotification(etat);
}
public Etatmachine GetEtat()
{
List<float> L = new List<float>();
DateTime Odate = DateTime.Now;
string OIdmachine = "P1-13";
DateTime dateEntrieur = Odate.AddDays(30);
var etats = db.vitesseis.Where(a => a.Idmachine == OIdmachine)
.Where(a => a.time <= Odate && dateEntrieur < a.time)
.OrderBy(a => a.time);
foreach (vitesseinstantaner reg in etats)
{
L.Add(reg.vitesse);
}
Etatmachine etat = new Etatmachine(OIdmachine, L.Last(), L);
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [idmachine],[time],[vitesse],
FROM [Bddcontext].[vitesseinstantaner] where max([time]) and [idmachine]=='" + OIdmachine + "'", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
{
reader.Read();
etat.vi = (int)reader["vi"];
etat.lastVis.Add(etat.vi);
if (etat.lastVis.Count >= 33)
etat.lastVis.RemoveAt(0);
}
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<EtatHub>();
return context.Clients.All.RecieveNotification(etat);
;
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
EtatHub nHub = new EtatHub();
nHub.GetEtat();
}
}
} |
et voici ma vue partiel qui sera appeler dans la index.html que sera
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <script src="@Url.Content("~/Scripts/")jquery-1.10.2.js"></script><script src="@Url.Content("~/Scripts/")jquery.signalR-2.2.0.min.js"></script>
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//creation du proxy
var itat= $.connection.etatHub;
itat.client.recieveNotification = function getData(data) {
var $etat = $('#blockdetat');
document.write("xxxxx")
$etat.empty();
$etat.append(' <div > ' + data.lastVis + '</div><div ">' + data.vi + '</div><div >' + data.id_machine + '</div>');
}
$.connection.hub.start().done(function () {
itat.server.getEtat();
}).fail(function (e) {
alert(e);
});
});
</script>
<div class="row-fluid">
<div class="span3 statbox green" onTablet="span6" onDesktop="span3">
<div id="blockdetat"></div>
</div>
</div> |
aussi j'ai ajouter a globl.asp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| protected void Application_Start() {
// call to RegisterBundles()
//...demarge de service broker
SqlDependency.Start(ConfigurationManager.ConnectionStrings["BddContext"].ConnectionString);
}
protected void Application_End()
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["BddContext"].ConnectionString);
} |
j'ai bien lu qui la documentions de signal http://www.asp.net/signalr/overview/...roubleshooting
mais j'arrive pas a résoudre mon problème
svp y'a quel q'un qui peut m'aides !! merci d'avance