I am starting to work with signalR but I get an error (I have seen several answers on the web but none has helped me), I have the following files
peopleHUB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace EjemploSignal
{
public class GenteHub:Hub
{
[HubMethodName("noti")]
public static void llenarDatos()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<GenteHub>();
context.Clients.All.updateDatos();
}
}
}
JS-file
var notificationHub = $.connection.genteHub;
$.connection.hub.start().done(function () {
cargarDatos();
});
$.notificationHub.client.updateDatos = function () {
cargarDatos();
};
function cargarDatos() {
$.ajax({
url: "~/Home/lista",
method: "GET",
async: false,
dataType: "json",
success: function (respuesta2) {
console.log("Respuesta", respuesta2);
}
});
}
Layout.html reference
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery.signalR-2.3.0.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/enlace.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
I have 2 errors that I get the first is the console:
According to several examples it is because I do not put the name of the class and others is that I must put the name of the [HubMethodName]
(I already put both and it does not matter)
The second error gives me in the Network:
if (typeof ($.signalR) !== "function") {
throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}
Which is according to why the file is not being generated, hub
either because:
- The correct order of the files is not being placed (When I have them in order according to "jquery, jquery signal, hub")
- The symbol (~) must be placed or not (the bad thing is that I already put it on and removed it and the error still persists)
What am I doing wrong?
you must remove
$.
from the variable