【发布时间】: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 项目并尝试它时会发生什么?