使用serverSentEvents

时间:2016-10-09 16:58:51

标签: jquery signalr

我收到serverSentEvent的解析响应错误。这是我的日志:

SignalR: Client subscribed to hub 'chalkhub'.
SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chalkhub%22%7D%5D'.
SignalR: serverSentEvents transport starting.
SignalR: Attempting to connect to SSE endpoint 'https://localhost:44300/signalr/connect?transport=serverSentEvents&clientPr…%2B1Ehw%3D%3D&connectionData=%5B%7B%22name%22%3A%22chalkhub%22%7D%5D&tid=2'.
SignalR: EventSource connected.
SignalR: serverSentEvents transport connected. Initiating start request.
SignalR: The start request succeeded. Transitioning to the connected state.
SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
SignalR: Invoking chalkhub.AddComment
SignalR: Invoked chalkhub.AddComment
SignalR error: Error: Failed at parsing response: {"C":"d-7520081E-B,2|F,0|D,0|G,1","M":[{
SignalR: Stopping connection.
SignalR: EventSource calling close().
SignalR: Fired ajax abort async = true.
SignalR: Stopping the monitoring of the keep alive.

以下是连接代码的集线器:

$.connection.hub.logging = true;
$.connection.hub.start().done(function() {
    console.log("ding");
});
$.connection.hub.error(function(error) {
    console.log('SignalR error: ' + error);
});
gAppStore.Hub.client.receivedNewComment = function(data) {
    var posts = this.state.data;
    var postFilter = $.grep(posts, function(p) {
        return p.Id === data.PostId;
    });
    postFilter[0].Comments.push(data);
    var newState = React.addons.update(this.state.data, {
        0: {
            Comments: {
                $set: postFilter[0].Comments
            }
        }
    });

    console.log(posts);
    this.setState({
        data: posts
    });
}.bind(this);

以下是中心服务器代码:

public void AddComment(Comment comment) {
    var ctx = new ApplicationDbContext();
    comment.CreatedAt = DateTime.Now;
    comment.UpdatedAt = DateTime.Now;

    ctx.Comments.Add(comment);
    ctx.SaveChanges();

    Clients.All.receivedNewComment(comment);
}

和评论模型是:

public class Comment {

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id {
        get;
        set;
    }

    public DateTime CreatedAt {
        get;
        set;
    }

    public DateTime UpdatedAt {
        get;
        set;
    }

    [Required]
    public Guid PostId {
        get;
        set;
    }

    [Required]
    public string CommentByUserId {
        get;
        set;
    }

    [Required]
    public string Text {
        get;
        set;
    }

    public virtual ApplicationUser CommentByUser {
        get;
        set;
    }

    public virtual Post Post {
        get;
        set;
    }
}

经过一些调试后,我发现 jquery.signalR-2.2.1.js 中的以下行中出现的确切错误:

connection.eventSource.addEventListener("message", function (e)

确切的错误是:

  

" JSON输入的意外结束"

和e.data值和connection.messageId是:

  

" {" C":" d-7520081E-H,1 | I,0 |百灵,0 | K,1"" M&# 34;:[{"

0 个答案:

没有答案