Search code examples
asp.netsignalrsignalr-hub

SignalR in ASP.NET empty website not working


I am very new to the term SignalR . I was try to configure the signalR into my empty website and Added following code.

In App_Code

  using Microsoft.Owin;
  using Owin;

  [assembly: OwinStartup(typeof(Super.App_Code.Startup))]

  namespace Super.App_Code
  {
     public class Startup
     {
         public static void Configuration(IAppBuilder app)
         {
             app.MapSignalR();

         }
     }
  }

and the hub class Inside App_Code

namespace Super.App_Code
{
[HubName("toastHub")]
 public class ToastHub : Hub
{
    public void BroadcastToast(string type ,string msg)
    {
        Clients.All.sendToast(type , msg);
    }
}
}

and in Default.aspx

<head runat="server">
<title></title>
<link rel="stylesheet" href="content/toastr.min.css" />

<script src="Scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-2.4.1.js" type="text/javascript"></script>
<script src="Scripts/toastr.min.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("#SuccessLink").click(function () {
            console.log("Success Click");
            $.connection.toastHub.client.broadcastToast('success', 'you have done');
        });

        $("#ErrorLink").click(function () {
            console.log("Error Click");
            $.connection.toastHub.client.broadcastToast("error", "you have not done");
        });

        $.connection.hub.start().done(function () {
            console.log('SignalR connected');
        })
        .fail(function (data) {
            console.log('SignalR failed to connect: ' + data);
        });

        $.connection.toastHub.client.sendToast = function (type, msg) {
            console.log("sendToast Called");
            toastr[type](msg);
        };
    });
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
    <div>
        <a id="SuccessLink" style="color: lightgreen">Success Toast</a><br />
        <a id="ErrorLink" style="color: red">Error Toast</a>
    </div>

  </form>
  </body>
  </html>

but when i run my website the javascript console says this when click on one of the tag

  SignalR connected
  Default.aspx:20 Success Click
  Default.aspx:21 Uncaught TypeError: $.connection.toastHub.client.broadcastToast is not a function
      at HTMLAnchorElement.<anonymous> (Default.aspx:21)
      at HTMLAnchorElement.handle (jquery-1.6.4.js:3001)
      at HTMLAnchorElement.eventHandle (jquery-1.6.4.js:2635)

Am i doing anything wrong please help me any help would be greatful .i am totally new to SignalR .

signalr/hub

if (typeof ($.signalR) !== "function") {
    throw new Error("SignalR: SignalR is not loaded. Please ensure         jquery.signalR-x.js is referenced before ~/signalr/js.");
}

var signalR = $.signalR;

function makeProxyCallback(hub, callback) {
    return function () {
        // Call the client hub method
        callback.apply(hub, $.makeArray(arguments));
    };
}

function registerHubProxies(instance, shouldSubscribe) {
    var key, hub, memberKey, memberValue, subscriptionMethod;

    for (key in instance) {
        if (instance.hasOwnProperty(key)) {
            hub = instance[key];

            if (!(hub.hubName)) {
                // Not a client hub
                continue;
            }

            if (shouldSubscribe) {
                // We want to subscribe to the hub events
                subscriptionMethod = hub.on;
            } else {
                // We want to unsubscribe from the hub events
                subscriptionMethod = hub.off;
            }

            // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
            for (memberKey in hub.client) {
                if (hub.client.hasOwnProperty(memberKey)) {
                    memberValue = hub.client[memberKey];

                    if (!$.isFunction(memberValue)) {
                        // Not a client hub function
                        continue;
                    }

                    // Use the actual user-provided callback as the "identity" value for the registration.
                    subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue);
                }
            }
        }
    }
}

$.hubConnection.prototype.createHubProxies = function () {
    var proxies = {};
    this.starting(function () {
        // Register the hub proxies as subscribed
        // (instance, shouldSubscribe)
        registerHubProxies(proxies, true);

        this._registerSubscribedHubs();
    }).disconnected(function () {
        // Unsubscribe all hub proxies when we "disconnect".  This is to ensure that we do not re-add functional call backs.
        // (instance, shouldSubscribe)
        registerHubProxies(proxies, false);
    });

    proxies['toastHub'] = this.createHubProxy('toastHub'); 
    proxies['toastHub'].client = { };
    proxies['toastHub'].server = {
    };

    return proxies;
};

signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
$.extend(signalR, signalR.hub.createHubProxies());

}(window.jQuery, window));

Solution

  • Looks to me like you need to just change the calls to broadcastToast from client to server. As that method is in the Hub and so requires the server instead.

    $.connection.toastHub.client.broadcastToast To $.connection.toastHub.server.broadcastToast