Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR For ASP.NET MVC with Angular 7 (Not ASP.NET core)

Hi I am trying to use signalR with asp.net mvc 5 and angular 7 , but unable to establish the connection between the hub

Here are my codes

Angular 7

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

@Component({
selector: 'app-admin-dashboard',
templateUrl: './admin-dashboard.component.html',
styleUrls: ['./admin-dashboard.component.scss']
})

export class AdminDashboardComponent implements OnInit {
private _hubConnection: HubConnection;
constructor() {
this._hubConnection = new HubConnectionBuilder()
  .withUrl("http://localhost:4200/hub/cashnet", {
    skipNegotiation: true,
    transport: $.HttpTransportType.WebSockets
  })
  .configureLogging($.LogLevel.Information)
  .build();

this._hubConnection.start().then(() => {
  console.log('Hub connection started');
}).catch(err => {
  console.log('Error while establishing connection :(')
});

this._hubConnection.on('sendMessage', (MachineId: string) => {
  alert(MachineId);
});
}


  ngOnInit(): void { 
}
}

Startup.cs

 public partial class Startup
 {
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        app.MapSignalR(); 
    }
 }

Hub/CashnetHub.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace CashNet_API.Hubs
{
public class CashnetHub : Hub
{
    public void sendMachineUpdate(string MachineId)
    {
        Clients.All.sendMessage(MachineId);
    }
 }
}

there is support for ASP.NET Core but we are not using core for this application

this is what i am getting in console Error in Console :img

thanks in advance

like image 530
Jayasooryan TM Avatar asked Sep 16 '25 23:09

Jayasooryan TM


1 Answers

You cannot use an ASP.NET Core SignalR client to connect to an ASP.NET SignalR server. They are incompatible. The @aspnet/signalr npm package is only for ASP.NET Core SignalR. You want to use https://www.npmjs.com/package/signalr for an ASP.NET SignalR server.

like image 159
Brennan Avatar answered Sep 19 '25 17:09

Brennan