【问题标题】:Flutter run websocket on background?Flutter在后台运行websocket?
【发布时间】:2018-05-26 03:07:38
【问题描述】:

我已经编写了一个基于 websocket 的应用程序,但是当我回到家(iOS 和 Android)时,websocket 就不再工作了。 如何使 websocket 仍然在后台工作?使服务可以一直运行。这是我的信息流和订阅:

channel = new IOWebSocketChannel.connect(wsUrl);
      globalChannel = channel;
      print("web socket channel connected.");

      // add channel.stream to streamController
      setState(() {
        _streamController = StreamController.broadcast();
        _streamController.addStream(channel.stream);
        globalStreamController = _streamController;
      });

      // Here we listen to ws stream and add to global messages
      // when enter chat page, globalSubscription should be stop
      // if globalSub were canceled, how to reopen it??
      globalSubscription = globalStreamController.stream.listen((data) {
}

如何让这个流控制器仍然运行而不是限制在当前页面类中?

【问题讨论】:

  • 看来要处理平台通道才能运行后台任务
  • 一般来说,你不能在iOS(无)或Android(受限)的后台运行任意代码。

标签: dart flutter


【解决方案1】:

围绕 SO 和网络的其他答案表明您不能只在后台保持套接字打开(这似乎是合理的,您会保持打开的网络连接可能会影响电池寿命)。根据您的用例,您可能会更好地查看推送通知或按计划检查的内容。

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2022-01-01
    • 2020-09-27
    相关资源
    最近更新 更多