如何在React Native应用中的iOS后台在后台运行socket.io? [英] How to run socket.io in the background on iOS in a React Native app?

查看:119
本文介绍了如何在React Native应用中的iOS后台在后台运行socket.io?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS React Native(v0.20) app中使用socket.io.该应用程序会跟踪我的位置,并且当我的位置发生变化时,它会向服务器发送一条消息.如果套接字连接丢失,服务器将发送电子邮件通知我.

I am using socket.io in an iOS React Native(v0.20) app. The app tracks my location, and when my position changes it emits a message to a server. If the socket connection is lost the server sends an email to notify me.

我使用 react-native-location 在后台进行位置跟踪,但我无法使socket.io正常工作.每当我更改应用程序或关闭屏幕时,该应用程序都会继续跟踪我的位置,但会丢失套接字连接.

I have the location tracking working in the background with react-native-location, but I can't get socket.io to work. When ever I change apps or turn off the screen, the app keeps tracking my location but I lose the socket connection.

是否可以像定位跟踪一样在后台运行socket.io?简短的说,有一些本机代码可以让我在后台维护client/server connection吗?

Is there a way to run socket.io in the background like location tracking ? Short of that is there some native code that will allow me to maintain a client/server connection while in the background?

我知道有一个 WebSocket替代,但我可以找不到让它在后台运行的方法.

I know there is a WebSocket alternative but I can't see a way to get it to run in the background.

更新: 我仔细检查了Info.plist,它已经为 react-native-location <设置了必要的背景值.我不知道这是否重要,但是套接字工作和位置跟踪是在同一组件中完成的.

UPDATE: I doubled checked my Info.plist, it has the necessary background values set already for react-native-location. I don't know if it matters but the socket work and location tracking are done in the same component.

LocationComponent.js

window.navigator.userAgent = 'react-native';
const io = require('socket.io-client/socket.io');
const socket = io(url, {jsonp: false});

import React, { Text, View, DeviceEventEmitter } from 'react-native';
import { RNLocation } from 'NativeModules';

export default GeolocationExample = React.createClass({

  componentDidMount: function() {
    RNLocation.requestAlwaysAuthorization();
    RNLocation.startUpdatingLocation();
    RNLocation.setDistanceFilter(3.0);
    DeviceEventEmitter.addListener('locationUpdated', locationObject => {
      this.props.newPosition({ longitude: locationObject.coords.longitude, latitude: locationObject.coords.latitude });
    });
  },

  render: function() {
    const { lastPosition, distance } = this.props;
    socket.emit('newPos', { longitude: lastPosition.longitude, latitude: lastPosition.latitude, distance, time: Date() });
    return (
      <View>
        <Text> {distance} </Text>
      </View>
    );
  }
});

推荐答案

您无法正确地"执行此操作.

There is no way you can do this "the right way".

修改

来自Apple的

执行长期运行的任务

Implementing Long-Running Tasks

对于需要更多执行时间才能实现的任务,您必须 请求特定权限以在后台运行它们而无需 他们被停职了.在iOS中,仅允许特定的应用类型 在后台运行:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

在后台播放可听内容的应用程序, 例如音乐播放器应用程序

Apps that play audible content to the user while in the background, such as a music player app

在录音机中录制音频内容的应用程序 背景

Apps that record audio content while in the background

使用户完全了解其位置的应用 时间,例如导航应用

Apps that keep users informed of their location at all times, such as a navigation app

支持互联网协议语音(VoIP)的应用

Apps that support Voice over Internet Protocol (VoIP)

需要定期下载和处理新内容的应用

Apps that need to download and process new content regularly

从外部附件接收定期更新的应用

Apps that receive regular updates from external accessories

实现这些服务的应用必须声明其服务 支持和使用系统框架来实现以下方面 这些服务.声明服务可让系统知道哪个 您使用的服务,但在某些情况下是系统框架 实际上可以防止您的应用程序被暂停.

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

话虽如此,但是有一种有效的技术可以保持您的套接字打开:

This being said, there is a working technique exist to keep your socket open :

播放静音,并将背景模式设置为音频.查看这篇文章有关此问题的更多信息(虽然有些陈旧,但仍然有效).这可以让您的插座保持打开状态,伪造音频应用程序.您的info.plist应该进行更新,以允许音频在后台运行. UIBackgroundModes应该设置为audio(检查

Play a silent sound and set the background mode to audio. Check this article for more information on the matter (it's a bit old but still valid). This will allow you to keep your socket open faking an audio app. Your info.plist should be updated to allow audio to run in the background. UIBackgroundModesshould be set to audio (check the docs for more information)

这篇关于如何在React Native应用中的iOS后台在后台运行socket.io?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆