简体   繁体   English

如何在 Flutter 中检查 tcp 套接字是否仍然连接?

[英]How to check if the tcp socket is still connected in Flutter?

I have connected to my server as follows:我已按如下方式连接到我的服务器:

Socket socket = await Socket.connect('xxx', xxx)

But now I want to check if the socket is still connected.但现在我想检查套接字是否仍然连接。 It would be even better if I have a listener that tells me when the connection is broken.如果我有一个监听器告诉我连接何时断开,那就更好了。

Thanks already!已经谢谢了! Leonard伦纳德

Listen to onDone in its stream在其流中收听 onDone

socket.stream.listen(
        (dynamic message) {
          debugPrint('message $message');
        },
        onDone: () {
          debugPrint('socket closed');//if closed you will get it here
        },
        onError: (error) {
          debugPrint('error $error');
        },
      );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Flutter应用程序连接到TCP套接字服务器? - How to connect Flutter app to tcp socket server? Flutter 如何检查是否有设备通过蓝牙连接到我的设备 - Flutter how to check if there is a device connected to my device via bluetooth Flutter 如何查看WIFI是否开启(连接与否无所谓)? - Flutter how to check if WIFI is turned on (connected or not it doesn't matter)? flutter_reactive_ble - 如何在订阅前检查设备是否已连接 - flutter_reactive_ble - how to check if device is connected before subscribing Flutter - 物理设备已连接到 USB 调试,但仍显示“未连接设备” - Flutter - Physical device is connected with USB debugging on but still shows "no devices connected" Flutter:TCP 套接字连接在真实设备上失败 - Flutter: TCP socket connection fails on real device 扑。 如何检查自动续订订阅是否仍然有效 - Flutter. How to check that autorenewal subscription is still valid Flutter 连接设备(医生检查崩溃) - Flutter Connected device (the doctor check crashed) Flutter 如何检查我的设备是否通过蓝牙连接到另一台设备 - Flutter How can I check if my device is connected to another device with Bluetooth or not 我如何解决颤振医生错误说已连接设备(医生检查崩溃)? - How do i solve flutter doctor error saying connected device (the doctor check crashed)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM