简体   繁体   中英

Making signalr generated proxy hubs url dynamic

I have a solution which need to connect using CORS to a signalr exposed service. The address where the signalr service will be hosted could change in time, so it's not suitable to have the classic script tag

<script src="http://server:8888/signalr/hubs" type="text/javascript"></script> 

but it would be fantastic if there's a way to reference the above url dynamically by javascript without the static script tag. Suggestions would be great!

Do the following in your JS file:

$.getScript('http://server:8888/signalr/hubs', function () {
    //Set the hubs URL for the connection
    $.connection.hub.url = 'http://server:8888/signalr';
    var hub = $.connection.yourHub; //yourHub is name of hub on the server side

    //wire up SignalR

    //start hub
    $.connection.hub.start().done(function () {
        //once we're done with SignalR init we can wire up our other stuff
    });
});

You could put your settings in a config file:

config.json :

{
    "signalr_url": "http://server:8888/signalr"
}

Then load the config:

$.getJSON('config.json', function(config) {
    $.getScript(config.signalr_url, function() {
        // when the script has loaded
    });
});

Edit:

After looking at the SignalR documentation , I think the following would be more suitable.

First include the needed scripts:

<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>

Now you can just call $.connection(...); with any url. So applying the above it could look like this:

$.getJSON('config.json', function(config) {
    $.connection(config.signalr_url);
});

I have this same scenario and after some researchs we've decided to put the generated proxy as a physical script like bellow:

How to create a physical file for the SignalR generated proxy

As an alternative to the dynamically generated proxy, you can create a physical file that has the proxy code and reference that file. You might want to do that for control over caching or bundling behavior, or to get IntelliSense when you are coding calls to server methods.

To create a proxy file, perform the following steps:

Install the Microsoft.AspNet.SignalR.Utils NuGet package.

Open a command prompt and browse to the tools folder that contains the SignalR.exe file. The tools folder is at the following location:

[your solution folder]\\packages\\Microsoft.AspNet.SignalR.Utils.2.1.0\\tools

Enter the following command:

signalr ghp /path:[path to the .dll that contains your Hub class]

The path to your .dll is typically the bin folder in your project folder.

This command creates a file named server.js in the same folder as signalr.exe.

Put the server.js file in an appropriate folder in your project, rename it as appropriate for your application, and add a reference to it in place of the "signalr/hubs" reference.

link: How to create a physical file for the SignalR generated proxy

As long we have proxy then you can just refer to the Hub url like this:

$.connection.hub.url = "http://your-url/signalr;
//open connection
$.connection.hub.start()
    .done(function () {
        //do your stuff
    })
    .fail(function () { alert('unable to connect'); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM