Here is what I did.
Problem is /signalr/hubs not being found (throws 404).
My project has areas and is structured as shown:
All the scripts for signalR are inside the Scripts folder and my SignalRTestController.cs looks like this:
namespace SignalRTest.Controllers
{
public class SignalRTestController : Controller
{
public ActionResult Index()
{
// Do some work here
// Broadcasting over a Hub from outside of a Hub
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.say("Hello SignalR!");
return View();
}
}
[HubName("MyHub")]
public class MyHub : Hub
{
public void Say(string message)
{
Clients.sendMessage(message);
}
}
}
Any my Index.cshtml has reference to all the javascripts and the /signalr/hubs too like below: // Other Javascripts
script type="text/javascript" src="/signalr/hubs" />
I think the controller is fine, but I'm not getting /signalr/hubs. It is throwing 404 and the message in Chrome Console is like this:
Resource interpreted as Script but transferred with MIME type text/html: "http://www.myproject.com/signalr/hubs". Uncaught SyntaxError: Unexpected token < hubs:2 Uncaught SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. script src='/signalr/hubs'>.
The reason the script is returned as html is the server is returning 404 not found.
I'm not sure what is wrong. I think this might be a routing issue. I'm not sure, if we need to add any routing information on the project for /signalr/hubs or am I missing something here.
FYI: When I create a new empty MVC project and add signalR and start working on it, it works perfectly fine. No need to add routing.
Also, I use both default routing and attribute routing in some places. But the controller SignalRTestController does not use attribute routing.
If your SignalR application transmits sensitive information between the client and server, use SSL for the transport.
SignalR provides an API for creating server-to-client remote procedure calls (RPC). The RPCs invoke functions on clients from server-side . NET Core code.
You can run SignalR on Https. SSL is a host concern, not really related to SignalR. Please check below mentioned links for more info.
Do you have a call to RouteTable.Routes.MapHubs() (probably in Global.asax)? If so, try getting rid of that and seeing if it fixes your problem. – Pete Nov 16 at 17:22
I've been playing with it some more. It appears that in the current version (I got the latest source because I needed a signed assembly), you have to call RouteTable.Routes.MapHubs(). But for it to work, it had to be called first (or at least before the RouteConfig.RegisterRoutes() call). If it was called after that, MVC goes hunting for a controller for it and that doesn't work. In the earlier version that I was using that got via nuGet, removing RouteTable.Routes.MapHubs() worked to fix the problem, but it now appears to be required. Hope that's helpful. - Pete Nov 27 at 20:53
Apparently you solved the problem by changing RouteTable.Routes.MapHubs() to: RouteTable.Routes.MapHubs("~/signalr").
MapHubs is now obsolete. Use MapSignalR extension method in the Owin startup class, like this:
[assembly: OwinStartupAttribute(typeof(signalr_test.Startup))]
namespace signalr_test
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR(); //can take path also see overloads...
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With