您可以连接到位于不同主机/服务器上的集线器吗?

Eva*_*tis 6 signalr signalr-hub

假设我在www.website.com上有一个网站.我的带信号器的SaaS托管在www.signalr.com上.

我可以从www.website.com连接到www.signalr.com信号服务器吗?

代替 :

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
Run Code Online (Sandbox Code Playgroud)

就像是 :

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('www.signalr.com/contosoChatHub');
Run Code Online (Sandbox Code Playgroud)

rad*_*tei 7

简短回答:是的 - 正如SinalR文档所示.

第一步是在服务器上启用跨域.现在,您可以启用来自所有域的呼叫,也可以仅启用指定的呼叫.(关于此事,请参阅此SO帖子)

    public void Configuration(IAppBuilder app)
        {
            var policy = new CorsPolicy()
            {
                AllowAnyHeader = true,
                AllowAnyMethod = true,
                SupportsCredentials = true
            };

            policy.Origins.Add("domain"); //be sure to include the port:
//example: "http://localhost:8081"

            app.UseCors(new CorsOptions
            {
                PolicyProvider = new CorsPolicyProvider
                {
                    PolicyResolver = context => Task.FromResult(policy)
                }
            });

            app.MapSignalR();
        }
Run Code Online (Sandbox Code Playgroud)

下一步是将客户端配置为连接到特定域.

使用生成的代理(请参阅文档以获取更多信息),您将连接到以TestHub下列方式命名的集线器:

 var hub = $.connection.testHub;
 //here you define the client methods (at least one of them)
 $.connection.hub.start();
Run Code Online (Sandbox Code Playgroud)

现在,您唯一需要做的就是指定在服务器上配置SignalR的URL.(基本上是服务器).

默认情况下,如果您未指定它,则假定它与客户端是同一个域.

`var hub = $.connection.testHub;
 //here you specify the domain:

 $.connection.hub.url = "http://yourdomain/signalr" - with the default routing
//if you routed SignalR in other way, you enter the route you defined.

 //here you define the client methods (at least one of them)
 $.connection.hub.start();`
Run Code Online (Sandbox Code Playgroud)

这应该是它.希望这可以帮助.祝你好运!