使信号器生成的代理集线器动态

时间:2022-09-03 21:22:10

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

我有一个解决方案需要使用CORS连接到信号器暴露的服务。寄存信号器服务的地址可能会随时间变化,因此不适合使用经典脚本标记

<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!

但如果有一种方法可以通过javascript动态引用上面的url而没有静态脚本标记,那就太棒了。建议会很棒!

3 个解决方案

#1


4  

Do the following in your JS file:

在JS文件中执行以下操作:

$.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
    });
});

#2


0  

You could put your settings in a config file:

您可以将设置放在配置文件中:

config.json:

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.

在查看SignalR文档后,我认为以下内容更合适。

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:

现在你可以调用$ .connection(...);任何网址。所以应用上面的内容可能如下所示:

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

#3


0  

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

如何为SignalR生成的代理创建物理文件

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.

作为动态生成的代理的替代方法,您可以创建具有代理代码并引用该文件的物理文件。您可能希望这样做是为了控制缓存或捆绑行为,或者在编写对服务器方法的调用时获取IntelliSense。

To create a proxy file, perform the following steps:

要创建代理文件,请执行以下步骤:

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

安装Microsoft.AspNet.SignalR.Utils NuGet包。

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

打开命令提示符并浏览到包含SignalR.exe文件的tools文件夹。 tools文件夹位于以下位置:

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

[您的解决方案文件夹] \ 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]

signalr ghp / path:[包含Hub类的.dll的路径]

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

.dll的路径通常是项目文件夹中的bin文件夹。

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

此命令在与signalr.exe相同的文件夹中创建名为server.js的文件。

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.

将server.js文件放在项目的相应文件夹中,根据应用程序重命名,然后添加对它的引用来代替“signalr / hubs”引用。

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

link:如何为SignalR生成的代理创建物理文件

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

只要我们有代理,那么您可以像这样引用Hub URL:

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

#1


4  

Do the following in your JS file:

在JS文件中执行以下操作:

$.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
    });
});

#2


0  

You could put your settings in a config file:

您可以将设置放在配置文件中:

config.json:

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.

在查看SignalR文档后,我认为以下内容更合适。

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:

现在你可以调用$ .connection(...);任何网址。所以应用上面的内容可能如下所示:

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

#3


0  

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

如何为SignalR生成的代理创建物理文件

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.

作为动态生成的代理的替代方法,您可以创建具有代理代码并引用该文件的物理文件。您可能希望这样做是为了控制缓存或捆绑行为,或者在编写对服务器方法的调用时获取IntelliSense。

To create a proxy file, perform the following steps:

要创建代理文件,请执行以下步骤:

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

安装Microsoft.AspNet.SignalR.Utils NuGet包。

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

打开命令提示符并浏览到包含SignalR.exe文件的tools文件夹。 tools文件夹位于以下位置:

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

[您的解决方案文件夹] \ 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]

signalr ghp / path:[包含Hub类的.dll的路径]

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

.dll的路径通常是项目文件夹中的bin文件夹。

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

此命令在与signalr.exe相同的文件夹中创建名为server.js的文件。

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.

将server.js文件放在项目的相应文件夹中,根据应用程序重命名,然后添加对它的引用来代替“signalr / hubs”引用。

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

link:如何为SignalR生成的代理创建物理文件

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

只要我们有代理,那么您可以像这样引用Hub URL:

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