How to use SignalR with a cross-domain

I am trying to use SignalR with a cross-domain, but I get an error when calling the start function. Error message"Uncaught TypeError: Cannot call method 'start' of undefined "

I use the server side code:

[assembly: OwinStartup(typeof(SignalRChat.Startup))]

    namespace SignalRChat
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.Map("/signalr", map =>
                {              
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration = new HubConfiguration
                    {                   
                       EnableJSONP = true
                    };               
                    map.RunSignalR(hubConfiguration);
                });
            }
        }
    }

Client code

 <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="/Scripts/jquery-1.6.4.min.js"></script>   
        <script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
    </head>
    <body>
       <div></div>
       <script type="text/javascript">
        var connection = $.connection.hub.url ='http://localhost:9370/signalr';      
        connection.hub.start()
          .done(function () {
              alert('Now connected, connection ID=' + connection.id);
          });
      </script>
      </body>
      </html>
+4
source share
2 answers

There are problems initializing and starting your Signalr connection, also declare a proxy to reference the hub. Example below:

<script src="/Scripts/jquery-1.6.4.min.js"></script>   
<script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
<script src="http://localhost:9370/signalr/hubs"></script>

 <script type="text/javascript">
    $.connection.hub.url ='http://localhost:9370/signalr';
    var yourHubProxy = $.connection.YourHubName;

    //Do something here with yourHubProxy

    $.connection.hub.start().done(function () {
        alert('Now connected, connection ID=' + $.connection.hub.id);
     });
 </script>

Another thing, I'm not sure why you are using different versions SignalRon the server side and on the client side. For me, you had SignalR 2.xyour server side and SignalR 1.1.4your side.

, SignalR -. http://damienbod.wordpress.com/2013/11/01/signalr-messaging-with-console-server-and-client-web-client-wpf-client/

+7

@Lin , , CROSS DOMAIN.

, , , localhost ex: http://localhost:9370

, , , http://dev-domain:9370/signalr/hubs HTTP- 400, .. .

, : Http://*: 8097

- , , :)

0

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


All Articles