【问题标题】:Flutter websocket connects to wrong portFlutter websocket连接到错误的端口
【发布时间】:2022-01-09 20:09:09
【问题描述】:

在我的应用程序中,我创建了一个用于与套接字通信的路由。

class _SocketRouteState extends State<SocketRoute> {
  @override
  void initState() {
    super.initState();

    try {
      WebSocketChannel _channel = IOWebSocketChannel.connect(
        Uri.parse('ws://192.168.1.90:9998'),
      );

      ///
      /// Start listening to new notifications / messages
      ///
      _channel.stream.listen(
        (data) {
          debugPrint(data);
        },
        onDone: () {
          debugPrint('ws channel closed');
        },
        onError: (error) {
          debugPrint('ws error $error');
        },
      );

      _channel.sink.add('testing');
    } catch (e) {
      ///
      /// General error handling
      /// TODO handle connection failure
      ///
      debugPrint('Connection exception $e');
    }
  }
}

但是,当我运行此代码时,它无法连接到套接字。等待约 2 分钟后,Xcode 显示以下错误:

flutter: ws error WebSocketChannelException: WebSocketChannelException: SocketException: OS Error: Operation timed out, errno = 60, address = 192.168.1.90, port = 52168

这清楚地显示了不同的端口。这可能是问题吗?有谁知道为什么它连接到端口52168 而不是9998

【问题讨论】:

  • 当你创建一个仅使用 main() 的 Dart 项目并尝试它时会发生什么?

标签: flutter dart websocket


【解决方案1】:

令人困惑的端口号的问题在于,错误消息不是那么好,因为它显示的是您自己机器上使用的本地端口,而不是您尝试连接的远程端口。 TCP 要求在服务器和客户端上都打开一个端口,以便它们可以双向通信。但通常情况下,您最感兴趣的是远程端口。

这里有一个关于这个问题的旧 github 问题: https://github.com/dart-lang/sdk/issues/12693

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 2021-01-15
    • 2021-01-14
    • 1970-01-01
    • 2014-01-21
    • 2016-09-05
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多