Shubh
Shubh

Reputation: 6741

$.connection.(hubname) is undefined. Signalr/hub 501(not implemented)

I have a MVC 4 application which has a register page.

The code for hub is as follows:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;

[HubName("messageHub")]
public class MessageHub : Hub
{
    /// <summary>
    /// Broadcast the message to all clients
    /// </summary>
    /// <param name="message">message to be broadcasted</param>
    public void Broadcast(string message, string messagetype, string messagetitle)
    {
        this.Clients.showMessage(message, messagetype, messagetitle);
    }
    public void Getsubscription()
    {

    }
}

Similarly, the JS hub code is:-

<script type="text/javascript" src="@Url.Content("~/Scripts/json2.js")"></script>
<script src= "@Url.Content("~/Scripts/jquery.signalR-0.5.2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
<h2>Index</h2>
<script type="text/javascript">
    $(document).ready(function () {
        //Initialize hub
        var hub = $.connection.messageHub;

        // hub.start();
        $.connection.hub.start();
        //Append event to Button
        //hub.broadcast("c","b","a");

    });

</script>

Now, after running it is get $.connection.messageHub as undefined

Screenshot in debug mode

secondly my chrome network shows;- Network -Chrome

Thirdly, when clicking on initiator for signalr/hub i get not implemented error:- enter image description here

I have tried changing the URL's for scripts, also ran as IIS but couldn't get it working. If i keep my scripts in .cshtml (View) and change --> use Local IIS Webserver, its working fine Any help.!

Upvotes: 3

Views: 3505

Answers (2)

x-6-2-5
x-6-2-5

Reputation: 1746

I found that adding reference to /signalr/hubs in bundle config dose not work because it is generated at run-time and bundling happens at compile-time.
so i did this:
1- created a section like this in each view i need to have reference to signalr/hubs:

    @section SignalRScripts{
    <script src="/signalr/hubs"></script>
    }  

2- in my layout file after rendering all other scripts, rendered this section:

    @Scripts.Render("~/js")  
    @RenderSection("SignalRScripts", required: false)  
    @RenderSection("Scripts", required: false) 

then it worked fine.

Upvotes: 1

Shekhar
Shekhar

Reputation: 31

@Scripts.Render("~/bundles/modernizr")

@Scripts.Render("~/bundles/jquery")

commented this section out in layout section and worked for me.

Upvotes: 3

Related Questions