Skip to content

Loading…

jquery.signalR.core.js - connection.stateChanged is called before state is actually changed #598

Closed
danderson00 opened this Issue · 6 comments

6 participants

@danderson00

Providing a callback for the stateChanged event, but the newState property of the argument does not match the actual connection state, so calling the server from this callback throws.

    $(function() {
        var server = $.connection.hubName;
        var connection = server._.connection();
        connection.stateChanged(function(change) {
            //change.newState !== connection.state
            if (change.newState === $.signalR.connectionState.connected)
                server.methodName(); // throws
        });
    });

A workaround to this is to wrap the server call in a setTimeout, but this is hardly ideal!

I notice in the code this is up for review. The method name is misleading for the current behaviour - "state changed" implies the state has already changed, but this is not the case.

@davidfowl
SignalR member

Yea, I'm not sure the current behavior is ideal but we need to change this behavior for all clients.

Aside:
In your code snippet, the supported way to get the connection is via $.connection.hub, not hubName._. If we change _ in th future your code will break.

@danderson00

No worries. Thanks for the heads up on access internals.

@danderson00

So is there a way to access the current connection state through the official API?

@davidfowl
SignalR member

For hubs:

$.connection.hub.state

$.connection.hub is the hub's connection.

$.connection.hub.stateChanged(function(change) {
});
@DamianEdwards DamianEdwards was assigned
@halter73 halter73 was assigned
@halter73 halter73 was assigned
@halter73 halter73 added a commit that referenced this issue
@halter73 halter73 Trigger stateChanged after the connection state has actually changed.
Fixes Issue #598 for the .NET client.
b0b2e81
@Xiaohongt
SignalR member

now in stateChanged(function (change) { ...} the change.oldState become same as change.newState

@halter73 halter73 added a commit that referenced this issue
@halter73 halter73 Fixed previous fixes to Issue #598
Fixed bug in JS and .NET clients where StateChanged events would be passed a
copy of the new state in lieu of the old state.
5d1e1ab
@Xiaohongt
SignalR member

verified

@Xiaohongt Xiaohongt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.