SignalR Connects fine first time, but when Reconnect is initiated, it doesn't complete Reconnect

Viewed 1220

I am using SignalR to push notifications to an angular application.

I have had it working before (although somewhat spotty it seemed), and now I am running into more issues regarding the reconnect.

I have gotten to the point where staring at the demo code of the documentation and others poeple code doesn't ever seem to work in SignalR like the claim it does. So I am posting my code to see if there is anything that I am blatently doing wrong that would not allow SignalR to work consistently and to start, reconnect, and disconnect consistantly.

I have a notification service that looks like the following:

.factory('Notification', ['$rootScope', '$log', '$localstorage', 'User', 'ENV', 'Events',
    function ($rootScope, $log, $localstorage, User, ENV, Events) {
        var self = this;

        this.proxy = null;
        this.connection = null;
        this.initializeSockets = function () {

            self.connection  = $.hubConnection(ENV.socketEndpoint, { useDefaultPath: true, logging: true })
            self.connection.qs = { token: User.getToken() };
            self.proxy = self.connection.createHubProxy('notificationEmitter');
            self.proxy.on('onNotification', function (notification) {
                $rootScope.$emit(notification.Topic, notification.Data);
            });

            if (self.connection.state === $.signalR.connectionState.disconnected) {
                self.connection.start(function () {})
                    .done(function (conn) {
                    $log.info('Connected to notification emitter');
                }).fail(function (a) {
                    $log.info('Unable to connect to connect:' + a)
                });
            }

            self.connection.starting = function (data) {
                $log.info('starting'); -> NEVER SEEMS TO GET CALLED
            }
            self.connection.disconnected = function (data) {
                $log.info('disconnected'); -> NEVER SEEMS TO GET CALLED
                setTimeout(function () {
                    proxy.connection.start();
                }, 20000); // Restart connection after 5 seconds
            };

            self.connection.reconnecting = function (data) {
                $log.info('reconnecting');
            }
            self.connection.reconnected = function (data) {
                $log.info('reconnected');
            }
            self.connection.connectionSlow = function (data) {
                $log.info('connectionSlow');
            }
            self.connection.error = function(error){
                $log.info('Signal R Error:' + error);
            }
        }

        $rootScope.$on(Events.userAuthenticated, function (event, user) {
            self.initializeSockets();
        })

        return self;

    }])

As I mentioned, it seems to connect most of the time the first time I start working on it, but after the first page refresh (and every time after) I get the following from the SignalR logs..

enter image description here

1 Answers
Related