Search code examples
asp.net-mvcangularsignalr

Angular/SignalR Error: Failed to start the connection. SyntaxError: Unexpected end of JSON input


Try to make simple angular/signalR application.

My Hub class:

namespace Gameserver.Hubs
{
    public class SignalRHub : Hub
    {
        public Task Send()
        {
            return Clients.All.InvokeAsync("Send", "Message from the server");
        }
    }
}

my startup.cs:

public partial class Startup
{
    public void Configure(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

and a component:

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));
  }

After running got this error from Loggers.js:

Error: Failed to start the connection. SyntaxError: Unexpected end of JSON input

Can anyone help me with this error?


Solution

  • After trying another two packages I found one which works fine. https://github.com/HNeukermans/ng2-signalR https://www.npmjs.com/package/ng2-signalr