yogesh singh
yogesh singh

Reputation: 105

use SignalR with cross domain

I am trying to use SignalR with cross domain but i am getting error message when calling start function. Error message is "Uncaught TypeError: Cannot call method 'start' of undefined "

I am using code Server side:

[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 side 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>

Upvotes: 1

Views: 1821

Answers (2)

Deepak Sharma
Deepak Sharma

Reputation: 4170

I am very late but just browsing abou SignalR, and just found this question now, so answering it now..

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

wrong is you are starting the hub connection.hub.start().. but in actual you need to start the connection not hub connection.start().

connection.start()
      .done(function () {
          alert('Now connected, connection ID=' + connection.id);
      });

if you trying cross domain SignalR this is the working code form my project..

          var con = $.hubConnection('http://localhost:50000/signalR');                                 
          var hub = con.createHubProxy('DataExchangeHub');          

          hub.on('OnMsgReceiveAll', function (message) {
              $('#message1').append('<li>' + message + '</li>');
          });
          hub.on('OnMsgReceiveClient', function (message) {
              $('#message2').append('<li>' + message + '</li>');
          });
          hub.on('OnMsgReceiveServer', function (message) {
              $('#message3').append('<li>' + message + '</li>');
          });

          con.start({ jsonp: true}).done(function () {
              $('#sendToAll').click(function () {
                  hub.invoke('BroadcastToAll', $('#msg').val());
              });
              $('#sendToClient').click(function () {
                  hub.invoke('BroadcastToClient', $('#msg').val());
              });
              $('#sendToServer').click(function () {
                  hub.invoke('BroadcastToServer', $('#msg').val());
              });
          });

      });

Upvotes: 2

Shachaf.Gortler
Shachaf.Gortler

Reputation: 5735

I believe you should enable cross domain on server like this , for SignalR 1.x

var config = new HubConfiguration
    {
        EnableCrossDomain = true
    };

For singalR 2.x look here

Upvotes: 0

Related Questions