Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a HTTP 404 not found error and when I try to run 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 have tried this: signalR : /signalr/hubs is not generated and it didnt work.

Any ideas?

like image 337
Niclas Gleesborg Avatar asked Oct 09 '14 08:10

Niclas Gleesborg


People also ask

What SignalR 2?

SignalR is a technology used to create real-time functionality to applications. The term real-time is the ability to get the content to the clients instantly from the server. It doesn't wait for the client to request the data.

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client.

Is SignalR two way communication?

SignalR uses Web Sockets and the HTML5 API that helps in bidirectional communication. It also provides an API for server-to-client Remote Procedure Calls (RPC) call, it may be something new for you because most of the time we use a request and response model.


2 Answers

In Global.asax file on the Application_Start event you have to register the hub url.

    protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
    }
like image 77
Aravind Sivam Avatar answered Oct 14 '22 15:10

Aravind Sivam


It worked for me:

Go to the startup class in your project, in the Configuration method and add this:

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

I hope it works for you

like image 30
Efrain Toribio Reyes Avatar answered Oct 14 '22 14:10

Efrain Toribio Reyes