Can you connect to a hub that is on a different host / server?

Say I have a website at www.website.com. My SaaS with signalr is available at www.signalr.com.

Can I connect to the signal server www.signalr.com from www.website.com?

Instead:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');

Sort of:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('www.signalr.com/contosoChatHub');
+5
source share
2 answers

Short answer: Yes - As the SinalR documentation illustrates.

The first step is to create a cross-domain on your server. Now you can activate calls from all domains or only from the specified ones. ( See this publication on this subject )

    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();
        }

The next step is to configure the client to connect to a specific domain.

(. ), TestHub

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

, , URL-, SignalR . ( ).

, , , , .

`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();`

. . !

+7

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


All Articles