gpt4 book ai didi

asp.net-core - Flutter 的 SignalR 集线器连接错误

转载 作者:行者123 更新时间:2023-12-05 07:10:27 27 4
gpt4 key购买 nike

我无法在我的 asp.net 核心后端调用我的 hub 方法,

这是我在 flutter 中的 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 core 中的 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.

最佳答案

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

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

因此,经过分析错误,确定是插件错误。你可以查看这个Issue以防你弄清楚。

关于asp.net-core - Flutter 的 SignalR 集线器连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61229508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com