Search code examples
c#signalrsignalr-hub

SignalR Webform - signalr/hubs Negociate issue


I have a legacy webform project in which is necessary to integrate signalr Technology I understood that the signalr/hubs is generated dinamically, however in my specific project i couldn make it work.

I tried to load all urls paths such as:

<script src="../signalr/hubs"></script>
<script src="~/signalr/hubs"></script>
<%--<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>--%>


To turn around this i generate the signalR in another project which is working fine and i copied to my scripts folder /Scripts/SignalR/Hub.js and i referenced it in client side.
<script src="../Scripts/SignalR/Hubs.js"></script>

It worked fine because my client could establish connection with my chatHub(serverside) initializing my chat variable as below:

enter image description here


However after that i got i error in the negociate process between server to client as the image below:

enter image description here



I think is because when the server trys to comunicate with the client it generates the url as [signalr/hub], but as i mentioned before, I'm using the /Scripts/SignalR/Hub.js file to instance de connection to the hub.

How can i fix this negociate issue between server to the client?

Here is my structure files

startup.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;
using SistemaJaspion.AppCode;
using System.Web.Routing;

[assembly: OwinStartup(typeof(SistemaJaspion.AppCode.Startup))]

namespace SistemaJaspion.AppCode
{
    /// <summary>
    /// The server needs to know which URL to intercept and direct to SignalR. To do that we add an OWIN startup class.
    /// </summary>
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            //app.MapSignalR("/signalr", new HubConfiguration());
            //app.MapSignalR();

            var hubConfiguration = new HubConfiguration();
            hubConfiguration.EnableDetailedErrors = true;
            hubConfiguration.EnableJavaScriptProxies = true;

            app.MapSignalR("/signalr", hubConfiguration);
        }

    }
}

ChatHub.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Web;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace SistemaJaspion.AppCode
{
    /// <summary>
    /// The Hub class is used to define methods the clients can call on the server
    /// </summary>
    /// 

    [HubName("chatHub")]

    //[Microsoft.AspNet.SignalR.Authorize]
    public class ChatHub : Hub
    {


        static List<UsuariosSignalR> ConnectedUsers = new List<UsuariosSignalR>();


        // Number of connected clients
        private static int connectionCount = 0;


        public void Send(string name, string message)
        {
            // Append the current date
            name = DateTime.Now.ToString() + " " + name;
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }

        public override Task OnConnected()
        {

            // Increase the number of connections
            Interlocked.Increment(ref connectionCount);

            // Update the number of the connected users
            Clients.All.reportConnections(connectionCount);
            return base.OnConnected();
        }


        public override Task OnDisconnected(bool stopCalled)
        {
            // Decrease the number of connections
            Interlocked.Decrement(ref connectionCount);

            // Update the number of the connected users
            Clients.All.reportConnections(connectionCount);
            return base.OnDisconnected(stopCalled);
        }   
    }
}

MasterPage.aspx


    <script src="../Scripts/jquery-1.6.4.min.js"></script>
    <script src="../Scripts/jquery.signalR-2.4.1.js"></script>
    <script src="../Scripts/SignalR/Hubs.js"></script>


    <!--Add script to update the page and send messages.-->
    <script type="text/javascript">


         $(function () {
                    /* Peer-to-peer broadcast code */
                    // Declare a proxy to reference the ChatHub.

             debugger;

                   var chat = $.connection.chatHub;

                    chat.client.reportConnections = function (count) {
                        $("#usersCount").text(count);
                    }

                    $.connection.hub.logging = true;
                    $.connection.hub.start();
                });

    </script>

Solution

  • I understood why it was not working.

    First of all, my research about signalR(Examples that I Studied) it was based on WebApplication/webforms project and in my case I was based on Wesite/webforms project which is a little differente struture between them.

    Anyway, The credit goes to Can SignalR be used with asp.net WebForms? where following the steps I could run signalR on website/webform .net project.