【问题标题】:SignalR For ASP.NET MVC with Angular 7 (Not ASP.NET core)SignalR For ASP.NET MVC with Angular 7(不是 ASP.NET 核心)
【发布时间】:2019-02-06 11:05:12
【问题描述】:

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

这是我的代码

角度 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,但我们没有为此应用程序使用核心

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

提前致谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2016-12-14
      • 2017-05-20
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多