在Angular4&中尝试使用SignalR时出错.NetCore 2.0应用程序

时间:2017-10-19 08:21:43

标签: c# angular typescript asp.net-core-2.0 asp.net-core-signalr

我试图在我的Angular 4& 4中使用SignalR 1.0.0 alpha2。 .NET Core 2.0应用程序,基于this文章。这是我的评论组件:

 import { Component, OnInit } from '@angular/core';
 import { AuthService } from "angular4-social-login";
 import { SocialUser } from "angular4-social-login";
 import { AuthenticationService } from "../services/auth/auth.service";
 import { Http } from '@angular/http';
 import { UserComment } from './UserComment';
 import { HubConnection } from '@aspnet/signalr-client';
 import { User } from "../login/User";

@Component({
    selector: 'app-comments',
    templateUrl: './comments.component.html',
    styleUrls: ['./comments.component.css']
})

export class CommentsComponent implements OnInit {

private loggedUserName: string = "";

private _hubConnection: HubConnection;
public async: any;

private commentTitle: string;
private commentText: string;
private commentAutor: string;

messages: string[] = [];

private Comments: Array<UserComment> = new Array<UserComment>();

constructor(private authService: AuthenticationService, 
    private httpService: Http) {
    this.checkUser();
    this.authService.getLoggedInName.subscribe(name => this.changeName(name));
}

private changeName(name: string): void {
    this.loggedUserName = name;
}

ngOnInit() {

    this._hubConnection = new HubConnection('http://localhost:4200/commentsPublisher');
    this._hubConnection.on('Send', (data: any) => {
        const received = `Received: ${data}`;
        this.messages.push(received);
    });

    this._hubConnection.start()
        .then(() => {
            console.log('Hub connection started')
        })
        .catch(err => {
            console.log('Error while establishing connection')
        });
}

但每当我尝试在客户端打开连接时,我都会收到错误:Error: Failed to start the connection. SyntaxError: Unexpected token G in JSON at position 0

当然,我的Startup.cs文件已经配置,并且已经创建了从HUB派生的类。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSignalR(routes =>
            {
                routes.MapHub<CommentsPublisher>("commentsPublisher");
            });

            app.UseCors("AllowAny");

            //another stuff
        }

我的评论发布者:

public class CommentsPublisher : Hub
{
    public Task PublishReport(string data)
    {
        return Clients.All.InvokeAsync("OnCommentsPublisher", data);
    }
}

那么,有什么想法吗?

更新:我修复了将软件包更新到4.4.6并拒绝身份验证的问题。

0 个答案:

没有答案