0

I am learning SignalR. Using a tutorial I managed to create a very simple ASP.NET MVC based SignalR Server. This is the code of the hub:

[HubName("echo")]
public class EchoHub : Hub
{
    public void Say(string message)
    {
        Trace.WriteLine(message);
    }
}

This is my Startup file:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                EnableJSONP = true
            };
            map.RunSignalR(hubConfiguration);
        });
    }
}

Next I created a HTML/JavaScript based client, that was in the same project as the server code. This client works correctly, when I debug the application, the Output window displays my message. This the code of the first client:

<script src="../Scripts/jquery-1.10.2.min.js"></script>
<script src="../Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="/signalr/hubs"></script>
<script>
        $(function() {
            var hubProxy = $.connection.echo;
            $.connection.hub.logging = true;

            $.connection.hub
                .start()
                .done(function () {
                    hubProxy.server.say('Hello SignalR');
                });
        })
</script>

The last thing I wanted to do is to create an client in separate project. I changed a little bit the code, mostly by adding the url's of the server. But this client don't work properly. This is the code of the client:

<script src="Scripts/jquery-1.6.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:51644/signalr/hubs"></script>
<script>
    $(function() {
        $.connection.hub.url = 'http://localhost:51644/signalr';
        var hubProxy = $.connection.echo;
        $.connection.hub.logging = true;

        $.connection.hub
            .start()
            .done(function () {
                hubProxy.server.say('Hello SignalR');
            });
    })
 </script>

The JavaScript don't even enters the function in the done method after starting the connection. But in console on Chrome there are no errors or exceptions being thrown.

So the question is what am I doing wrong in the second client?

EDIT: I enabled and checked the SignalR client logs in Chrome console. The second client is stoping by step "SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%5D'." No errors are returned, everything seems alright, but the client cannot go past this step.

EDIT2: According to the comment from JF Beaulieu the negotiate return 200 status code, but nothing else. After the negotiate there should be next steps, like invoking Say, but in the external client hangs on the negotiate. I paste here console outputs from the build in and external JavaScript client.

Build in client output:

enter image description here

External client output:

enter image description here

The second screenshot shows, that the client stops executing on the negotiate step.

I've also tried the solution of Mareq, but adding the jsonp attribute didn't help.

EDIT3: I add here a link to my Github repo with this project, the server is in the AspNetServer project and client in JavaScriptClient project, hope that helps: https://github.com/RomanSuska/SignalRSandbox

4
  • if you access http://localhost:51644/signalr/hubs do you see some generated javascript? Commented Feb 18, 2016 at 14:54
  • @JFBeaulieu Yes, I see generated JavaScript code, the files from the first and second client are equal. Commented Feb 18, 2016 at 18:57
  • What transport are you using to connect? Can you see a 200 OK in the network tab of dev tools for the Negotiating with... message? Commented Feb 18, 2016 at 20:24
  • I have updated the question to answer your comment. Commented Feb 19, 2016 at 8:08

3 Answers 3

2

I finally found the solution of my problem and it's stupid simple. The problem was with the SignalR JavaScript file version, on the server I used version 2.1.2 of SignalR and on the client I used 2.2.0. When I switched the version of the client to 2.1.2 everything started to work.

Sign up to request clarification or add additional context in comments.

Comments

1

My case was due to my Hub having a constructor because I was trying to do Dependency Injection with Unity. After I removed the constructor, everything worked.

3 Comments

I'd been tearing my hair out about this for so long. This was exactly my problem too! Any idea how to implement DI in this case?
Seems like you don't. Haven't figured it out. I haven't touched C# after the project that needed this also so I'm not sure if it's even possible xD
In the end I managed to do something along the lines of this post damienbod.com/2013/11/05/using-signalr-with-unity
0

Add this argument to start:

$.connection.hub.start({ jsonp: true });

EDIT I hosted SignalR in console app and I used simple set configuration:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR(new HubConfiguration { EnableJSONP = true });
    }
}

and client code I used like this:

$(function () {
        var hub = $.connection.serviceHub;
        $.connection.hub.url = 'http://mareq.ddns.net:8087/signalr';

        $.connection.hub.start({ jsonp: true });

        ...
    });

5 Comments

This would be a much better answer if you explained what this change does. What's the purpose of jsonp?
This didn't resolve the problem, the client JavaScript still hangs on the negotiating step
According to your update, I've tried it on a console application like you and it is working fine, but if I make a ASP.NET MVC server (with the same hub and startup code), the client fails to connect. Could you try to make a MVC server?
mareq.pl - server [hub code as a previously] mareq.tk - client [js you can preview]
I managed to find the solution, I thank you for your help, your project were helpfull for finding the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.