SignalR js client seems to be ignoring the url port

Ken Smith picture Ken Smith · Nov 16, 2012 · Viewed 9.4k times · Source

I'm trying to put together a simple "Hello World" style application with SignalR. The slightly complicating factor is that the SignalR hubs need to be self-hosted, not in IIS/ASP.NET. I've got the server-side working, so far as I can tell, and it's available on port 8080, but I'm having trouble wiring up the client. The problem I'm bumping up against at the moment is that the SignalR client seems to be ignoring the port on the URL that I specify.

Specifically, I've got this code here:

<head runat="server">
    <script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="/Scripts/json2.min.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.signalR-0.5.3.js"></script>
    <script type="text/javascript" src="http://<%=Request.Url.Host %>:8080/signalr/hubs"></script>
    <title>SignalR Test</title>
</head>
<body>
    <script type="text/javascript">
        $(function () {

            // Wire up the client to the SignalR server on the same host 
            // as the source of this page, but on port 8080.
            $.connection.url = "http://<%=Request.Url.Host %>:8080/signalr";
            var roomHub = $.connection.roomHub;

            $('#echoButton').click(function () {
                roomHub.echo($('#echoButton').val())
                    .error(function (err) {
                        alert(err);
                    });
            });

            $.connection.hub.start({ transport: 'auto', xdomain: true })
                .done(function () {
                    console.log('Connected.');
                })
                .fail(function (e) {
                    console.log('Unable to connect:' + e);
                });
        });
    </script>

The :8080/signalr/hubs script loads successfully, and it looks good, i.e., it has the definition for the roomHub in it, so I know that the server is up and running.

However, when $.connection.hub.start() runs, it seems like it should try to open up a connection for a URL something like http://app.dev.alanta.com:8080/signalr/signalr/negotiate?=1353072553935. Instead, Firebug tells me that it's ignoring the 8080 portion, and is instead trying to negotiate a connection with the URL http://app.dev.alanta.com/signalr/signalr/negotiate?=1353072553935. And of course, that doesn't work - there's no SignalR service listening on port 80, just the regular web server - so it fails with the message, "Unable to connect:SignalR: Error during negotiation request".

I should also note that in the jquery.signalR-0.5.3.js file, the bit of code that parses out the connection does indeed seem to ignore the port:

// Resolve the full url
parser.href = connection.url;
if (!parser.protocol || parser.protocol === ":") {
    connection.protocol = window.document.location.protocol;
    connection.host = window.document.location.host;
    connection.baseUrl = connection.protocol + "//" + connection.host;
}
else {
    connection.protocol = parser.protocol;
    connection.host = parser.host;
    connection.baseUrl = parser.protocol + "//" + parser.host;
}

// Set the websocket protocol
connection.wsProtocol = connection.protocol === "https:" ? "wss://" : "ws://";

Is this a bug? Or have I misunderstood something?

Answer

Ken Smith picture Ken Smith · Nov 16, 2012

Well, I could swear that I'd tried this and it didn't work, but as I was troubleshooting it some more, I changed the URL assignment from this:

$.connection.url = "http://<%=Request.Url.Host %>:8080/signalr";

To this:

$.connection.hub.url = "http://<%=Request.Url.Host %>:8080/signalr";

And to be sure, this is how it's documented here. I thought that I'd seen it documented the first way somewhere, but I can't find it now. Oh well. Chalk this one up to my not paying enough attention.