Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross domain requests not working in SignalR 2.0.0-rc1

Tags:

c#

cors

signalr

I recently upgraded a project from SignalR 2.0.0-beta1 to 2.0.0-rc1. I understand that in RC1, configuration of support for cross domain requests changed. I've updated my project to use the new syntax however I'm now getting the following error when attempting to communicate with my hub:

XMLHttpRequest cannot load =1377623738064">http://localhost:8080/negotiate?connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&clientProtocol=1.3&=1377623738064. Origin http://localhost:7176 is not allowed by Access-Control-Allow-Origin.

The client site is running at http://localhost:7176 and the hub is listening via a console application at http://localhost:8080. Am I missing something here? Cross domain requests were working before I upgraded to RC1.

CONSOLE APP ENTRY POINT

static void Main(string[] args)
{
    var chatServer = new ChatServer();
    string endpoint = "http://localhost:8080";

    chatServer.Start(endpoint);

    Console.WriteLine("Chat server listening at {0}...", endpoint);
    Console.ReadLine();
}

CHATSERVER CLASS

public class ChatServer
{
    public IDisposable Start(string url)
    {
        return WebApp.Start<Startup>(url);
    }
}

STARTUP CONFIGURATION

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            map.RunSignalR(new HubConfiguration { EnableJSONP = true });
        });
    }
}
like image 635
Scott Avatar asked Aug 27 '13 17:08

Scott


2 Answers

Something is wrong with your client configuration.

XMLHttpRequest cannot load =1377623738064">http://localhost:8080/negotiate?connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&clientProtocol=1.3&=1377623738064. Origin http://localhost:7176 is not allowed by Access-Control-Allow-Origin.

The negotiate request should be made to http://localhost:8080/signalr/negotiate?... not http://localhost:8080/negotiate?.... To fix this you can try the following before you call $.connection.hub.start:

$.connection.hub.url = http://localhost:8080/signalr;

like image 185
halter73 Avatar answered Sep 28 '22 01:09

halter73


Not sure if this question has been adequately answered, but I made the following changes to the sample provided by Microsoft:

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

And I added the following to the JS sample:

$.connection.hub.start({ jsonp: true }).done(function () {
    $('#sendmessage').click(function () {
        // Call the Send method on the hub.
        chat.server.send($('#displayname').val(), $('#message').val());
        // Clear text box and reset focus for next comment.
        $('#message').val('').focus();
    });
});

And now the Cross domain scripting is enabled. Hope this helps someone else, I was really puzzling with it for a while.

like image 37
user2760821 Avatar answered Sep 28 '22 02:09

user2760821