SignalR For ASP.NET MVC with Angular 7 (Not ASP.NET core)
SignalR For ASP.NET MVC with Angular 7 (Not ASP.NET core)
您好,我正在尝试将 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 核心,但我们没有为此应用程序使用核心
这就是我在控制台中得到的
Error in Console :img
提前致谢
您不能使用 ASP.NET Core SignalR 客户端连接到 ASP.NET SignalR 服务器。他们是不相容的。 @aspnet/signalr npm 包仅适用于 ASP.NET Core SignalR。您想将 https://www.npmjs.com/package/signalr 用于 ASP.NET SignalR 服务器。
您好,我正在尝试将 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 核心,但我们没有为此应用程序使用核心
这就是我在控制台中得到的 Error in Console :img
提前致谢
您不能使用 ASP.NET Core SignalR 客户端连接到 ASP.NET SignalR 服务器。他们是不相容的。 @aspnet/signalr npm 包仅适用于 ASP.NET Core SignalR。您想将 https://www.npmjs.com/package/signalr 用于 ASP.NET SignalR 服务器。