Creating url dynamic proxy signature hubs

I have a solution that needs to be connected using CORS to the signal related service. The address where the signalr service will be hosted may change over time, so it is not suitable for the classic script tag

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

but it would be great if there was a way to link to the above URL dynamically using javascript without a static script tag. Suggestions would be great!

+4
source share
3 answers

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
    });
});
+4
source

You can put your settings in the configuration file:

config.json

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

:

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

Edit:

SignalR, , .

:

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

$.connection(...); URL-. , , :

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

, script :

SignalR -

, - . , IntelliSense, .

, :

Microsoft.AspNet.SignalR.Utils NuGet.

, SignalR.exe. :

[ ]\\Microsoft.AspNet.SignalR.Utils.2.1.0\

:

signalr ghp/path: [ DLL, Hub]

.dll bin .

server.js , signalr.exe.

server.js , "signalr/hubs".

link: SignalR

-, URL- :

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

Source: https://habr.com/ru/post/1534643/


All Articles