如何在FlutterbackgroundServices方法中接收插座事件[Flutter]

发布于 2025-02-07 17:17:40 字数 869 浏览 0 评论 0原文

我正在使用节点JS中的服务器运行socket.io,我想做的是,当应用程序在后台时,它仍然从插座接收事件,只能发送事件,但不会接收事件。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  socketConnection.socket.connect();
  await FlutterBackgroundService.initialize(onStart); //here start executing the onStart method
  runApp(MyApp());
}


onStart() async {
  WidgetsFlutterBinding.ensureInitialized();
  final service = FlutterBackgroundService();
  if (await service.isServiceRunning()) {
    socketEvents();//socket events method
  }
}

socketEvents() {
  log(socketConnection.socket.connected.toString()); //here I want to know if it is connected and it always shows me **false**, but it does connect
  socketConnection.socket.on('noty_event', (data) async {
    log(data['msg'].toString()); //I want to show in the console when an event arrives but it does not show anything
  });
}

I am using a server in node js to run socket.io, what i want to do is that when the app is in the background it still receive events from the sockets, can only send events, but does not receive them.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  socketConnection.socket.connect();
  await FlutterBackgroundService.initialize(onStart); //here start executing the onStart method
  runApp(MyApp());
}


onStart() async {
  WidgetsFlutterBinding.ensureInitialized();
  final service = FlutterBackgroundService();
  if (await service.isServiceRunning()) {
    socketEvents();//socket events method
  }
}

socketEvents() {
  log(socketConnection.socket.connected.toString()); //here I want to know if it is connected and it always shows me **false**, but it does connect
  socketConnection.socket.on('noty_event', (data) async {
    log(data['msg'].toString()); //I want to show in the console when an event arrives but it does not show anything
  });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文