locked
How to apply cross domain connection on SignalR RRS feed

  • Question

  • User-134102843 posted

    I'm currently working on SignalR and i found out that there is connection limitation of browser (can only open up to 6 tabs).

    So, i've looked up for a lot of suggestion for the workaround and eventually i decided to go for multiple subdomain methods.

    After i've implemented the subdomain method (creating a lot of subdomain binding to the same ip address example: bla.msg.com,bla1.msg.com,bla2.msg.com and etc), it did overcome the limitation problem however, having the URL changing(having messy URL such as bla5.msg.com) is not something i want. So, i looked through the documentation of SignalR and i found out that i might be able to establish connection to the hub using cross domain method, which means that i dont have to redirect my URL(having messy URL) but stay with same URL but having different connection to my subdomain hub (bla1.msg.com/signalr/hubs , bla2.msg.com/signalr/hubs and etc.) 

    I've been trying to do that by changing my javascript from 

     <script type="text/javascript" src="/signalr/hubs"></script>

    to

     <script type="text/javascript" src="http://www.bla1.msg.com/signalr/hubs"></script>

    I checked on the chrome socket monitor and the connection of bla1.msg.com is there but it is not active while the connection of bla.msg.com is active.

    I've tried the things that mentioned on the documentation including the following code (included in startup.cs):

    app.Map("/signalr", map =>
    {
    // Setup the CORS middleware to run before SignalR.
    // By default this will allow all origins. You can
    // configure the set of origins and/or http verbs by
    // providing a cors options with a different policy.
    map.UseCors(CorsOptions.AllowAll);
    var hubConfiguration = new HubConfiguration
    {
    // You can enable JSONP by uncommenting line below.
    // JSONP requests are insecure but some older browsers (and some
    // versions of IE) require JSONP to work cross domain
    EnableJSONP = true
    };
    // Run the SignalR pipeline. We're not using MapSignalR
    // since this branch already runs under the "/signalr"
    // path.
    map.RunSignalR(hubConfiguration);
    });

    So, it seems that the cross domain is not working the way i expected it to be (which is having active web sockets on bla1.msg.com in this case). Am i on the wrong track or is there any idea why the cross domain is not working? Thank you !

    Thursday, April 20, 2017 9:19 AM

All replies

  • User-1838255255 posted

    Hi zstay,

    About the apply a cross-domain connection in SignalR 1.X , please refer to the following code:  

    var hubConfiguration = new HubConfiguration();
    hubConfiguration.EnableCrossDomain = true;
    RouteTable.Routes.MapHubs(hubConfiguration);

    About apply a cross-domain connection in SignalR 2.X, please use Microsoft.Owin.Cors.

    for more details, please refer to this document:

    How to establish a cross-domain connection:

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain 

    Best Regards,

    Eric Du

    Friday, April 21, 2017 9:36 AM
  • User-134102843 posted

    Hi Eric,

    I'm currently using SignalR 2.X and i've tried using the  Microsoft.Owin.Cors but it doesnt seem to work.It seems that it would not allow me to specify the url by this statement:

    $.connection.hub.url = "/signalr"

     Is there any other methods of your recommendation?

    Thank you!

    Best Regards,

    zstay

    Wednesday, April 26, 2017 12:19 AM