Ng2-signalr Error: Failed to connect. Error: Error during negotiation request

im using this lib and i set up signlaR as this demo and nothing works, but when i give way to the pageComponent it can't connect to SignalRServer, the server works, i use it for other things but signalR doesn't work. did I set up something wrong?

My app.module.ts

[...]
//external libs
import { SignalRModule } from 'ng2-signalr';
import { SignalRConfiguration } from 'ng2-signalr';
import { ConnectionResolver } from "app/route.resolver";

//ng2-signalr v2.0.0
export function createConfig(): SignalRConfiguration {
  const c = new SignalRConfiguration();
  c.hubName = 'Ng2SignalRHub';
  c.qs = { user: 'donald' };
  c.url = 'http://localhost:54755';
  c.logging = true;
  return c;
}

@NgModule({
    declarations: [
        AppComponent,
        HomePage,
        LoginPage,
        CustomerPage,
        ConfigurationPage,
        OfferPage,
                [...]
                DocumentModalAcceptComponent,
        OfferModalComponent,
        OfferCliModalComponent
         ],
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        Routing,
        SignalRModule.forRoot(createConfig)
    ],
    providers: [
        ConnectionResolver,
        AuthService,
        { provide: LOCALE_ID, useValue: 'pt-BR' }
    ],
    bootstrap: [AppComponent]
})

      

my page.component.ts

import { SignalRConnection, BroadcastEventListener, ConnectionStatus } from 'ng2-signalr';
import { ActivatedRoute } from "@angular/router";

@Component({
    selector: 'app-offer-cli',
    templateUrl: './offer-cli.component.html',
    providers: [DataService,Helper]
})
export class OfferCliComponent implements OnInit {
    private errors: any[] = [];
    private modalIsOpen: boolean;
    [...]
    private user: any;
    private data: any;

    private _connection: SignalRConnection;
    private chatMessages: ChatMessage[];
    private statuses: any;



    constructor(private service: DataService, private helper: Helper, private route: ActivatedRoute) {
          this._connection = this.route.snapshot.data['connection'];
       }

    ngOnInit() {

        // 1.create a listener object
        let onMessageSent$ = new BroadcastEventListener<ChatMessage>('OnMessageSent');

        // 2.register the listener
        this._connection.listen(onMessageSent$);

        // 3.subscribe for incoming messages
        onMessageSent$.subscribe((chatMessage: ChatMessage) => {
            this.chatMessages.push(chatMessage);
        }); 

        this._connection.status.subscribe((status: ConnectionStatus) => {
            this.statuses.push(status);
        });

        //this._connection.start();

        this._connection.errors.subscribe((error: any) => {
            this.errors.push(error);
            console.log("erro de conexão com o signalR")
            console.log(error)
        });
    }
[...]
export class ChatMessage {
  constructor(public user: string, public content: string) {}
}  

      

my app.routing.ts

[...]
import { ConnectionResolver } from './route.resolver';

import { AuthService } from './services/auth.service';

const appRoutes: Routes = [
    { path: '', component: LoginPage },
    { canActivate: [AuthService], path: 'home', component: HomePage },
    { canActivate: [AuthService], path: 'customer', component: CustomerPage },
    { canActivate: [AuthService], path: 'offer', component: OfferPage },
    { canActivate: [AuthService], path: 'client/offer', component: OfferPageCli,resolve: { connection: ConnectionResolver } },
    { canActivate: [AuthService], path: 'configuration', component: ConfigurationPage },
    { canActivate: [AuthService], path: 'contact', component: ContactPage },
    { canActivate: [AuthService], path: 'document', component: DocumentPage }

];

export const RoutingProviders: any[] = [];
export const Routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

      

my route.resolver.ts

import { Resolve } from '@angular/router';
import { SignalR, SignalRConnection } from 'ng2-signalr';
import { Injectable } from '@angular/core';

@Injectable()
export class ConnectionResolver implements Resolve<SignalRConnection> {

    constructor(private _signalR: SignalR)  { }

    resolve() {
        console.log('ConnectionResolver. Resolving...');
        return this._signalR.connect();
    }
}

      

My .angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "macaw-web-backoffice"
  },
  "apps": [
    [...]
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/signalr/jquery.signalR.js"
        ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
[...]

      

I am using jquery@3.2.1 ng2-signalr@2.0.6 signalr@2.2.2

this is my hub.cs

namespace Macaw.WebApi.hubs
{
    [HubName("Ng2SignalRHub")]
    public class Ng2SignalRHub : Hub 
    {

        public override Task OnConnected()
        {

            Trace.TraceInformation("Ng2SignalRHub - OnConnected");

            var user = GetAuthenticatedUser();

            Clients.All.OnUserConnected(user);

            return base.OnConnected();
        }

        private string GetAuthenticatedUser()
        {
            var username = Context.QueryString["user"];
            if (string.IsNullOrWhiteSpace(username))
                throw new System.Exception("Failed to authenticate user.");

            Trace.TraceInformation("GetAuthenticatedUser :" + username);

            return username;
        }


        // chat
        public void Chat(ChatMessage message)
        {
            // broadcast to all clients
            Clients.All.OnMessageSent(new ChatMessage("Hannes", "Message"));
        }



    }

    public class ChatMessage
    {
        public ChatMessage(string user, string message)
        {
            User = user;
            Message = message;
        }

        public string User { get; set; }
        public string Message { get; set; }
    }
}

      

I am not installing anything about hubs in the controller

when i open my OfferCliComponent i got it in Imgur console thank you very much

+3


source to share





All Articles