React Native 不从父应用程序调用 Android 本机方法
2018-07-20
1115
我正在尝试启动并运行我的 React Native Redis 模块, 但出现以下错误。当我尝试引用任何函数时
TypeError: undefined is not an object (evaluating '_reactNativeRedispubsub.RedisClient.redisConnect')
RedisClient.js
/**
* This exposes our Android code into our RN project
*/
import { NativeModules } from "react-native";
module.exports = NativeModules.RedisClient;
通过 redison jar 实际调用 redis 的文件 RedisClient.java
package com.redispubsub;
import android.util.Log;
import android.widget.Toast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.redisson.client.RedisConnectionException;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
public class RedisClient extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private RedissonClient mredissonClient;
public RedisClient(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RedisClient";
}
@ReactMethod
public void redisConnect(String ipPort) {
//Redisson client setup to be done in background thread
try {
Config config = new Config();
config.useSingleServer().setAddress(ipPort); //IPAddress of Redis and port
mredissonClient = Redisson.create(config);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@ReactMethod
public void subscribe(String channelName) {
if (mredissonClient != null) {
RTopic subscribeTopic = mredissonClient.getTopic(channelName, StringCodec.INSTANCE); //Channel to listen and Codec.
subscribeTopic.addListener(new MessageListener < String > () {
@Override
public void onMessage(String channel, String msg) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(channel, msg);
}
});
} else {
}
}
@ReactMethod
public long publish(String ChannelName, String msg) {
long clientsReceivedMessage = 0;
if (mredissonClient != null) {
//Redisson publish
RTopic topic = mredissonClient.getTopic(ChannelName, StringCodec.INSTANCE);
try {
clientsReceivedMessage = topic.publish(msg); //Publishing lat lon to Coros
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
}
return clientsReceivedMessage;
}
}
将方法公开给 Java 脚本 RedisCleintPackage.java
package com.redispubsub;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
public class RedisClientPackage implements ReactPackage {
@Override
public List < NativeModule > createNativeModules(ReactApplicationContext reactContext) {
List < NativeModule > modules = new ArrayList < > ();
modules.add(new RedisClient(reactContext));
return modules;
}
public List < Class << ? extends JavaScriptModule >> createJSModules() {
return Arrays.asList();
}
@Override
public List < ViewManager > createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
这个 _reactNativeRedispubsub.RedisClient.redisConnect 是否意味着它没有获取要调用的正确对象?
我很困惑为什么它没有拾取 RedisClient.java 中的 redisConnect
整个代码都在我的 react-native android 项目中运行,其中我运行着 MainActivity,但是当我将它发布到 npm 并通过 npm 安装时,它却不起作用
2个回答
我明白了
需要在 redisclient.js 中进行以下更改
/** * 这会将我们的 Android 代码公开到我们的 RN 项目中 */
import { NativeModules } from "react-native";
const { RedisClient } = NativeModules;
export {
RedisClient
}
Nikhil Kulkarni
2018-07-20
我使用类似这样的方法:
import {NativeModules} from 'react-native';
module.exports = NativeModules.RedisClient;
Paul
2018-07-23