Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR OnConnected and OnDisconnected not firing

I'm having trouble with OnConnected and OnDisconnected overrides in my Hub not firing.

For replication purposes, I've got a very simple Hub:

public class OnlineHub : Hub
{
    public void TestMethod()
    {
        var a = 1;
    }

    public override System.Threading.Tasks.Task OnConnected()
    {
        return Clients.All.connected(Context.ConnectionId, DateTime.Now.ToString());
    }

    public override System.Threading.Tasks.Task OnDisconnected()
    {
        return Clients.All.disconnected(Context.ConnectionId, DateTime.Now.ToString());
    }
}

And an aspx page:

<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            $.connection.onlineHub.server.testMethod();
        });

        $.connection.onlineHub.server.connected = function (id, date) {
            $("#results").append("connected: " + id + " : " + date + "</br>");
        };

        $.connection.onlineHub.server.disconnected = function (id, date) {
            $("#results").append(("connected: " + id + " : " + date + "</br>");
        };

        $.connection.onlineHub.connection.start();
    });

</script>

I'm using jQuery 1.6.4 and signalR 1.0.0-alpha2. The client side connected and disconnected methods are not being executed. And if I put a breakpoint in OnConnected or OnDisconnected, the breakpoints don't hit.

The connection is being made as my TestMethod calls ok and I'm getting a connectionId back from signalR's negotiate call.

I'm sure I'm missing something simple.

Thanks in advance.

like image 353
DavidGouge Avatar asked Dec 10 '12 10:12

DavidGouge


People also ask

Why does SignalR disconnect?

Client disconnection scenarios When the user closes a browser window or tab, or navigates to a new page or refreshes the page, the SignalR connection immediately ends because SignalR client code handles that browser event for you and calls the Stop method.

Is SignalR full duplex?

SignalR supports full duplex communication that allows the client and server to transfer data back and forth.

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.

What is a SignalR error?

This error may be seen if the project contains a folder called SignalR, which will interfere with the automatically-created proxy. To avoid this error, do not use a folder called SignalR in your application, or turn automatic proxy generation off.


2 Answers

It also turns out that on a script you need to define at least one client callback for events to be raised on the hub. Be careful with this when expected OnConnected and OnDisconnected to be called.

like image 168
Thulani Chivandikwa Avatar answered Sep 21 '22 12:09

Thulani Chivandikwa


Your client handlers should probably look like this:

$.connection.onlineHub.client.connected = function(){...}
like image 25
Duncan Smart Avatar answered Sep 17 '22 12:09

Duncan Smart