颤抖在背景上运行websocket?

时间:2018-05-26 03:07:39

标签: dart flutter

我已经编写了一个基于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) {
}

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

1 个答案:

答案 0 :(得分:1)

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