SignalR 2 does not generate / signalr / hubs

Here is the page:

<script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="~/signalr/hubs"></script> <!--SignalR script to update the chat page and send messages.--> <script> $(function () { // Reference the auto-generated proxy for the hub. var notification = $.connection.notificationHub; // Create a function that the hub can call back to display messages. notification.client.addNewMessage = function (message) { // Add the message to the page. $('#discussion').append('<li><strong>' + '</strong>: ' + htmlEncode(message) + '</li>'); }; // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); // This optional function html-encodes messages for display in the page. function htmlEncode(value) { var encodedValue = $('<div />').text(value).html(); return encodedValue; } </script> 

Here is the hub class:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; namespace AdminWebApp.Hubs { [HubName("notificationHub")] public class NotificationHub : Hub { public void SendNotification(string message) { Clients.All.addNewMessage(message); } } } 

Startup.cs:

 using Microsoft.Owin; using Owin; [assembly: OwinStartupAttribute(typeof(AdminWebApp.Startup))] namespace AdminWebApp { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } } } 

When I try to access: http://localhost:4551/signalr/hubs I get an HTTP 404 error not found, and when I try to start the page, I get:

  Failed to load resource: the server responded with a status of 404 (Not Found) Uncaught TypeError: Cannot read property 'client' of undefined 

I tried this: signalR: / signalr / hubs is not generated and it did not work.

Any ideas?

+5
source share
5 answers

In the Global.asax file of the Application_Start event, you need to register the hub URL.

  protected void Application_Start() { RouteTable.Routes.MapHubs(); } 
+2
source

Try this and no need to write a line in the App-Start Event

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Owin; using Microsoft.Owin; [assembly: OwinStartup(typeof(Faceless_Books.Hubs.Startup))] namespace Faceless_Books.Hubs { public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } } } 
+3
source

This worked for me:

Go to the launch class in your project in Configuration Method and add the following:

app.MapSignalR ("/ signalr", new HubConfiguration ());

I hope this works for you.

+3
source

If you are using Visual Studio 2012, the SignalR Hub Class (v2) template will not be available. Instead, you can add a simple class called ChatHub.

step by step

1) In Solution Explorer, right-click the project, select **Add** | SignalR Hub Class (v2). Name the class ChatHub.cs In Solution Explorer, right-click the project, select **Add** | SignalR Hub Class (v2). Name the class ChatHub.cs

2) Change class

 using System; using System.Web; using Microsoft.AspNet.SignalR; namespace SignalRChat { public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } } } 
0
source

In my case, I had copy / paste of the class name from the example, but this is not what was called by my class.

JS:

 var notification = $.connection.notificationHub; // <--- my class is not called "NotificationHub" 

FROM#:

 public class ToastHub : Hub // <--- should have been $.connection.toastHub above { ... } 
0
source

Source: https://habr.com/ru/post/1204311/


All Articles