【发布时间】: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(受限)的后台运行任意代码。