颤振的SignalR集线器连接错误

时间:2020-04-15 13:05:55

标签: asp.net-core flutter websocket signalr invalid-argument

我无法在asp.net核心后端

中调用我的hub方法。

这是我的Fluke中的CommentHub:

import 'package:signalr_client/signalr_client.dart';
import 'package:logging/logging.dart';   
import 'package:xperience/models/global.dart';

final serverUrl = "http://" + base + ":8070/CommentHub/";

final hubConnection = HubConnectionBuilder().withUrl(serverUrl).build();

class HubService {
static final hubProtLogger = Logger("SignalR - hub");
static final  transportProtLogger = Logger("SignalR - transport");
static final connectionOptions = HttpConnectionOptions;

static final httpOptions = new HttpConnectionOptions(logger: transportProtLogger);
final hubConnection = HubConnectionBuilder().withUrl(serverUrl, options: h 
httpOptions).configureLogging(hubProtLogger).build();

Future addUser(String UserId,String PostId) async {
Logger.root.level = Level.ALL;
 Logger.root.onRecord.listen((LogRecord rec) {
  print('${rec.level.name}: ${rec.time}: ${rec.message}');
});

hubConnection.start().then((result) async {
  final result=await hubConnection.invoke("AddUser",args:<Object>[UserId,PostId]);
  print(result);
});
}

Future removeUser() async {
final result = await hubConnection.invoke("RemoveUser",args: <Object>[]);
print(result);
hubConnection.stop();

}
}

这是我在asp.net核心中的CommentHub.cs类:

      using System;
  using Microsoft.AspNetCore.SignalR;
  using Microsoft.AspNet.SignalR.Hubs;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using System.Linq;
  using Xperience.Data.Entities.Posts;

 namespace Xperience.Hub
  {
  public class CommentHub : Microsoft.AspNetCore.SignalR.Hub
   {
    public static List<Connection> Connections = new List<Connection>();

    public void AddUser(string UserId,string PostId) {
        Connections.Add(new Connection
        {
            ConnectionId = Context.ConnectionId,
            UserId = UserId,
            PostId = int.Parse(PostId)
        });
    }
    public void RemoveUser() {
        var user = Connections.Find(x => x.ConnectionId == Context.ConnectionId);
        Connections.Remove(user);
    }

    }

    public class Connection { 
    public string ConnectionId { get; set; }
    public string UserId { get; set; }
    public int PostId { get; set; }
    }
     }

我想在用户进入并离开页面时调用AddUser和RemoveUser,但是每当我调用addUser时,都会出现此错误:

**[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s)
and Sometimes : 'GeneralError' is not a subtype of type 'Error' in type cast**

尽管连接已成功完成:

HTTP send: url 'http://ipAddress:8070/CommentHub/?id=XAekJZiTdIqswThHnPHWRQ', method: 'POST' content: '{"type":6}'
I/flutter (17474): FINEST: 2020-04-15 15:54:48.840894: (SSE transport) request complete. Response status: 200.

1 个答案:

答案 0 :(得分:0)

这似乎是signalR flutter客户端插件的问题。我收到类似的错误。我的代码最少。 我这边的错误来自启动功能

void startConnection() async {
    hubConnection.start();
}

因此,在分析了该错误之后,我确信这是一个插件错误。 您可以检查一下此Issue,以防万一。