为什么即使我们取消订阅,thingsboard 流仍然会继续收听?

问题描述 投票:0回答:0

我写了我的代码类似以下链接的 web socket API 示例: https://thingsboard.io/docs/reference/dart-client/

在我的代码中,我提供了一个命令来获取一段时间内的遥测数据并将它们存储在固定大小的数组中,之后,我每 60 秒获取一次最新的遥测数据并将该遥测数据添加到我的数组中。在互联网连接良好之前,我的应用程序运行良好。当互联网断开时,我在 Android studio 的控制台中看到“WebSocket 错误:错误代码 - 1002”,在这种情况下获取遥测一段时间,再次完成!这导致我的阵列溢出!

    // Create subscription command with entities query and timeseries subscription
    var cmd = EntityDataCmd(query: devicesQuery, tsCmd: tsCmd);

    // Create subscription with provided subscription command
    var telemetryService = tbClient.getTelemetryService();
    var subscription = TelemetrySubscriber(telemetryService, [cmd]);

    // Create listener to get data updates from WebSocket
    subscription.entityDataStream.listen((entityDataUpdate) {
      print('Received entity data update: $entityDataUpdate');
    });
    // Perform subscribe (send subscription command via WebSocket API and listen for responses)
    subscription.subscribe();

    // Wait few seconds to show data updates are received by subscription listener
    await Future.delayed(Duration(seconds: 2));

    // Finally unsubscribe to release subscription
    subscription.unsubscribe();

我检查了我的代码,我知道即使我在获取一段时间的遥测数据后写了

subscription.unsubscribe();
,我仍然继续收听流! 我怎么能停止收听 Stream?

flutter stream listener subscription thingsboard
© www.soinside.com 2019 - 2024. All rights reserved.