2

I am using SignalR in an ASP.Net Web Application project and am having issues. My goal is to make any changes in the administrative side of the site cause some GridViews to refresh. I was planning on doing this by sending the signal for some javascript to be run, thereby refreshing the update panels containing the GridViews.

The issue right now is that I cannot get any of the code my hub is trying to call to execute in the client. I am receiving the following error in FireBug from the jquery.SignalR.js file, but I'm not sure how to proceed to fix it:

Firefox can't establish a connection to the server at ws://localhost:40068/signalr?data=[]&transport=webSockets&clientId=92e4f7b9-0118-4fd9-bb55-5f22338d6162.

(function(n,t){"use strict";if(typeof ...on=n.signalR=i})(window.jQuery,window)

After it throws this error it still looks like it is setting up the connection, but none of the javascript being sent through the hub is executed

I have set up the following hub in my site:

namespace testProject
{
    public class statusChanges : Hub
    {
        public void ServerChange()
        {
            Clients.serverChange();

        }
    }
}

I have the following code in my button click event in the admin section. Debugging shows that this code is being run by the server:

var clients = Hub.GetClients<statusChanges>();
clients.serverChange();

Finally I have this code in my page trying to just launch an alert when it receieves the signal to confirm it is working.

<script type="text/javascript">
    $(function () {
        var statusChange = $.connection.statusChanges;
        statusChange.serverChange = function () {
            alert(8);
        };
        $.connection.hub.start();
    });
</script>

Does any one have any ideas why this would not run or what the FireBug error means?

2
  • i am a little confused, is the serverChange javascript function on the same page as the button that causes a postback? are you using two instances of the browser to test? The firebug error is a non-issue as you correctly stated, the connect gets setup anyway using long-polling instead of web sockets afaik. Commented Nov 2, 2011 at 20:40
  • There are two different pages; the admin one causing the event to fire and the viewer that should be getting the alert. I am viewing it in two different browsers. Commented Nov 2, 2011 at 22:00

1 Answer 1

5

The error in firebug is expected. It's the websocket connection failing, don't worry about it as SignalR will fallback to longpolling. You have a method on the server side with the same name as a client side event. That doesn't work.

You want something like this:

public Administration : Hub {
}

Event handler:

var clients = Hub.GetClients<Administration>();
clients.serverChange();

Javascript:

<script type="text/javascript">
    $(function () {
        var administration= $.connection.administration;
        administration.serverChange = function () {
            alert(8);
        };
        $.connection.hub.start();
    });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

The HttpContext line was actually just put there to test if it was doing the function. I realized it was trash and planned to take it out; I just forgot to take it out before posting my code. I will try your answer though. It looks like I don't need anything in the hub at all if I do my javascript this way?
Yep, you just need the hub to generate the client side proxy ($.connection.{hubname}) in this case.
I'm facing this exact same issue presently on my local machine (even if SignalR does still work with the error). The var clients = Hub.GetClients<Administration>(); clients.serverChange(); part of your example - where does this go?
I am using same type of script with a file upload which is actually upload from server to another server. so just server events. and seems to be failing in same fashion. and Client not able to see the progress bar moving. It moves based on server progress with serverChange method. How much time there in long polling ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.