适用于Angular 7的SignalR For ASP.NET MVC(非ASP.NET核心)

时间:2019-02-06 11:05:12

标签: asp.net-mvc-5 signalr angular7 signalr-hub

嗨,我正在尝试将SignalR与asp.net mvc 5和angular 7一起使用,但无法在集线器之间建立连接

这是我的密码

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);
    }
 }
}

有对ASP.NET Core的支持,但是我们没有为此应用程序使用core

这就是我在控制台中得到的 Error in Console :img

预先感谢

1 个答案:

答案 0 :(得分:0)

您不能使用ASP.NET Core SignalR客户端连接到ASP.NET SignalR服务器。它们不兼容。 @ aspnet / signalr npm软件包仅适用于ASP.NET Core SignalR。您想将https://www.npmjs.com/package/signalr用于ASP.NET SignalR服务器。