【问题标题】:Angular/SignalR Error: Failed to start the connection. SyntaxError: Unexpected end of JSON inputAngular/SignalR 错误:无法启动连接。 SyntaxError:JSON 输入意外结束
【发布时间】: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


【解决方案1】:

在尝试了另外两个包后,我发现一个可以正常工作。 https://github.com/HNeukermans/ng2-signalR https://www.npmjs.com/package/ng2-signalr

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2018-08-24
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2017-02-18
    • 2021-01-21
    相关资源
    最近更新 更多