Search code examples
jqueryasp.net-mvc-4signalrsignalr-hubsignalr.client

Signal R client code is not invoking the HUb


I am using Visual studio 2010 and jquery signal r 1.1.4 for sending notification like we have in facebook. Below is the Hub method used

public class BroadcastMessage:Hub
    {
        public void BroadcastNotifications(string message)
        {
            Utility.AddNotification(message); //to save data to database

            int UnreadCount = Utility.getUnreadMessageCount(); //get the unread count 

            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastMessage>();
            context.Clients.All.receiveNotification(message, UnreadCount);
        }
        public void SendMessages()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastMessage>();
            context.Clients.All.updateMessages();
        }
    }

And the partial view where i have the text bx to enter the notification message and the send button is @model MyProject.Models.Notification

@

using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Notification</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Message)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LogonID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LogonID)
            @Html.ValidationMessageFor(model => model.LogonID)
        </div>
        <p>
            <input type="button" id="broadcastNotification" value="Send" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
<script src="~/Scripts/jquery-1.12.0.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
    $(function () {
        $.connection.hub.logging = true;
        var proxy = $.connection.broadcastMessage;

        $.connection.hub.start().done(function () {
            $('#broadcastNotification').click(function () {
                proxy.server.broadcastNotifications($("#Message").val()).done(function () {
                    proxy.server.sendMessages();
                });
            });
        });

    });
</script>
}

Global.asax file is

 protected void Application_Start()
        {
            //To register the default Hubs route
            RouteTable.Routes.MapHubs();
}

here my signal r hub method is not getting triggered on click of the Send button. I tried http://localhost:XXXX/signalr there i got error "Protocol error: Unknown transport." can some one show me what mistake i have made here.


Solution

  • I suggest you to add some logging in your startup :

    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
            ConfigureAuth(app);
            app.MapSignalR(hubConfiguration);          
        }
    }