【发布时间】:2018-02-22 14:06:06
【问题描述】:
尝试制作简单的 angular/signalR 应用程序。
我的 Hub 类:
namespace Gameserver.Hubs
{
public class SignalRHub : Hub
{
public Task Send()
{
return Clients.All.InvokeAsync("Send", "Message from the server");
}
}
}
我的startup.cs:
public partial class Startup
{
public void Configure(IAppBuilder app)
{
app.MapSignalR();
}
}
还有一个组件:
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr-client';
export class CoreComponent implements OnInit
{
private connection: HubConnection;
ngOnInit(){
this.connection = new HubConnection('http://localhost:49756/signalr/');
this.connection.on('send', data =>{
console.log(data);
})
this.connection.start()
.then(() => console.log('connected'))
.catch(err => console.log('Error while establishing connection: ' + err));
}
运行后从Loggers.js得到这个错误:
Error: Failed to start the connection. SyntaxError: Unexpected end of JSON input
谁能帮我解决这个错误?
【问题讨论】:
-
当您通过浏览器直接访问“localhost:49756/signalr/”时,是否需要任何身份验证?
-
不,不需要
-
我遇到了完全相同的问题,是的,我的集线器需要身份验证,有什么解决方案吗?
-
试试看我的答案:该解决方案也包含身份验证数据。
标签: asp.net-mvc angular signalr