Trying to set up a simple connection using SignalR in Angular 6, I wrote the following code:
signalR.helper.ts
public static setupHub<T>(hubUrl: string, eventName: string, callback: (data: T) => void, ...params: SignalRParam[]): HubConnection {
const token = localStorage.getItem(appConstant.token);
const url = this.buidlUrl(hubUrl, ...params);
const connection = new HubConnectionBuilder()
.withUrl(url,
{ transport: HttpTransportType.WebSockets, accessTokenFactory: () => token })
.build();
environment.production && connection.start();
!environment.production && connection.start().catch(err => console.error(err.toString()));
connection.on(eventName, callback);
return connection;
}
However, when trying to log in on my page, I keep encountering this error message in the console:
signalR.helper.ts:19 Error: Cannot send data if the connection is not in the 'Connected' State.
I'm relatively new to SignalR and Angular. Can someone explain why I am getting this error?