Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor of class 'HubConnection' is private and only accessible within the class declaration

I am trying to implement an angular and signalr project. I am getting the sample from Medium

I have installed all the necessary packages and have the code below in the app.component.ts:

import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';

import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'PROJECT TITLE';
  base_url = 'http://localhost:8081/';

  hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { 

  }

  ngOnInit(): void {
    this.hubConnecton = new HubConnection(this.base_url);
  }
}

But I get an error in the = new HubConnection(this.base_url); with this message: Constructor of class 'HubConnection' is private and only accessible within the class declaration.

enter image description here

I need your help with this please. Thank you.

like image 820
Ibanez1408 Avatar asked Aug 15 '18 02:08

Ibanez1408


1 Answers

You have to use HubConnectionBuilder().

import { Component, OnInit } from '@angular/core';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  public _hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { }

  ngOnInit(): void {
    this._hubConnecton = new HubConnectionBuilder().withUrl("http:/:localhost:1874/notify").build();
    this._hubConnecton
      .start()
      .then(() => console.log('Connection started!'))
      .catch(err => console.log('Error while establishing connection :('));

    this._hubConnecton.on('BroadcastMessage', (type: string, payload: string) => {
      this.msgs.push({ severity: type, summary: payload });
    });
  }
}
like image 101
Virag Shakhida Avatar answered Oct 07 '22 13:10

Virag Shakhida