最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Making signalr generated proxy hubs url dynamic - Stack Overflow

programmeradmin6浏览0评论

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!

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!

Share Improve this question edited Apr 3, 2014 at 6:21 Hoghweed asked Apr 2, 2014 at 11:52 HoghweedHoghweed 1,9481 gold badge16 silver badges36 bronze badges 2
  • Why not make the script tag less static: <script src="<%= SignalRUrl %>...? – sroes Commented Apr 3, 2014 at 6:35
  • Sure it can be possible but is not a so elegant and cleaner way to it..now i'm doing that registering a clientscriptreference in the page ScriptManager, but I'd like too much if that's could be acplished by javascript, so the scripting part is not so coupled with the code-behind one! – Hoghweed Commented Apr 3, 2014 at 6:48
Add a ment  | 

3 Answers 3

Reset to default 4

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 mand 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 mand:

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 mand 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'); });
发布评论

评论列表(0)

  1. 暂无评论