24

I am trying to get a super simple hub connection working cross-domain but having no luck. I've read dozens of posts and done everything mentioned but still no success.

My server hub is here

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.broadcastMessage(name, message);
    }
}

My server MapHubs call is here

RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });

Any my javascript client is here

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-2.0.1.min.js"></script>
    <script src="~/Scripts/jquery.signalR-1.1.2.min.js"></script>
    <script src="/signalr/hubs"></script>
</head>
<body>
    <div class="container">
        <input type="text" id="displayname" value="Test" />
        <input type="text" id="message" value="I'm here" />
        <input type="button" id="sendmessage" value="Send" />
    </div>
    <script type="text/javascript">
        $(function ()
        {
            $.connection.hub.url = 'http://<my url>/';
            var chat = $.connection.chatHub;
            alert(chat);
            $.connection.hub.start().done(function ()
            {
                alert("Connection succeeded");
            }).fail(function ()
            {
                alert("Connection failed");
            });
        });
    </script>
</body>
</html>

The problem is that it never reaches the Connection succeeded or failed alerts and the alert(chat) call returns undefined.

I've tried several combinations for the $.connection.hub.url line

$.connection.hub.url = 'http://<My url>';
$.connection.hub.url = 'http://<My url>/';
$.connection.hub.url = 'http://<My url>/signalr';
$.connection.hub.url = 'http://<My url>/signalr/';

The developer console in Chrome and Firebug give me the error

Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>. 

On the same domain it works fine. This is really starting to drive me crazy so any help would be appreciated.

Thanks, Jason

Jason
  • 2,455
  • 4
  • 37
  • 48

1 Answers1

21

Your server is being hosted cross domain yet you're trying to get the hubs from the current domain. Therefore it's failing to retrieve the hubs file and you don't actually have a proxy to work with (which is why everything is not working).

So you have two options:

  1. Manually create the hubs file and host it on the current domain: http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#manualproxy.
  2. Use the raw hub connection API and do not include the signalr/hubs file at all.

Here's a code snippet of how you can use the raw hub connection API: http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#nogenconnection (second code snippet).

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • Awesome, thanks! I had actually tried using the manual proxy method (2) and had no luck but I must have had something else wrong at the time. I tried it again and got it working. Now to get it working on PhoneGap but that's a challenge for another day. – Jason Jun 06 '13 at 14:34
  • @Jason - any luck with phonegap and signalr? Looking to crack this one next week, would be helpful to hear someone else has made it work –  Jan 15 '15 at 17:55
  • @DeeMac - we ended up not going ahead with the project (client funding issue) so I haven't really done any more with it. From what I've seen SignalR has evolved quite a bit since I last worked with it. Have fun though, it's a pretty cool technology. – Jason Jan 16 '15 at 00:14
  • @Jason - no problem, thanks very much for your reply anyway. It is pretty cool actually, I'm enjoying it. –  Jan 16 '15 at 07:36