0

I am able to connect to my Hub and I've hooked up OnConnected and OnDisconnected. They should add/subtract from a integer and call a client callback with the new value. My angular application is connecting to the server successfully but my registered callback function is not being triggered.

Here is my Serverhub:

[HubName("online")]
public class OnlineHub : Hub
{
    private static int userCount = 0;

    public override Task OnConnected()
    {
        userCount++;
        Clients.All.listUpdated(userCount);

        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {

        userCount--;

        Clients.All.listUpdated(userCount);

        return base.OnDisconnected(stopCalled);
    }
}

And here's my Angular SignalRService:

import { AppSettings } from './../app.settings';
import { EventEmitter, Injectable, OnDestroy } from '@angular/core';

declare const $: any;

@Injectable()
export class SignalRService {

  // Declare the variables
  private onlineHub: any;
  // create the Event Emitter
  public messageReceived: EventEmitter<any>;
  public connectionEstablished: EventEmitter<Boolean>;
  public connectionExists: Boolean;

  constructor(private appSettings: AppSettings) {
    // Setup
    this.connectionEstablished = new EventEmitter<Boolean>();
    this.messageReceived = new EventEmitter<any>();
    this.connectionExists = false;
  }

  // This method gets executed from angular controller
  public initialize(proxyName: string): void {
    this.onlineHub = $.connection.online;

    this.onlineHub.client.listUpdated = function(list: any): void {
      console.log(list);
      this.messageReceived.emit(list);
    };

    this.startConnection();
  }

  private startConnection(): void {
    $.connection.hub.url = this.appSettings.SIGNALR_BASE_URL + '/signalr';
    $.connection.hub.start()
      .done((data: any) => {
        console.log('SignalR Connected with: ' + data.transport.name);
        this.connectionEstablished.emit(true);
        this.connectionExists = true;
      })
      .fail((error: any) => {
        console.log('SignalR could not connect: ' + error);
        this.connectionEstablished.emit(false);
      });
  }

  private registerOnServerEvents() {
    this.onlineHub.client.listUpdated = function(list: any): void {
      console.log(list);
      this.messageReceived.emit(list);
    };
  }
}

I am registering my callback "listUpdated" before I run start() as the documentation says and $.connection.hub contains client.listUpdated before start() is called so it should register. But still, the OnConnected method is not called.

2
  • Which method is not being hit? The server side OnConnected(), the client side listUpdated() handler, or both? Commented Oct 10, 2018 at 16:39
  • @Learning2Code Both, but the error is probably on the serverside OnConnected() since that method in turn is calling listUpdated() on the client. Commented Oct 11, 2018 at 13:30

1 Answer 1

1

I fixed this issue by surrounding the OnConnected() and OnDisconnected() code in try/catch block and created a clientside method called "error" that returns eventual exceptions to the client. That way I found out that I had a Json Serialization issue.

My Hub now looks like this:

    [HubName("online")]
    public class OnlineHub : Hub
    {
        private static int userCount = 0;


        public override Task OnConnected()
        {
            try
            {
                userCount++;
                Clients.All.listUpdated(userCount);

            }
            catch (Exception exc)
            {
                Clients.All.error(exc);
            }

            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            try
            {
                userCount--;
                Clients.All.listUpdated(userCount);
            }
            catch (Exception exc)
            {
                Clients.All.error(exc);
            }

            return base.OnDisconnected(stopCalled);
        }
    }

And I register the error callback on the js client BEFORE calling start():

this.onlineHub.client.error = (exc: any): void => {
  console.log('Error occured:', exc);
};
Sign up to request clarification or add additional context in comments.

Comments

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.