Angular / SignalR错误:无法启动连接。 SyntaxError:JSON输入的意外结束

时间:2018-02-21 13:48:04

标签: asp.net-mvc angular signalr

尝试进行简单的角度/信号应用。

My 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

任何人都可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

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