简体   繁体   English

如何从JavaScript获取React Native packager ip?

[英]How to get React Native packager ip from JavaScript?

Is there any way to get dynamicly React Native packager ip with port or full path to bundle - jsCodeLocation through JavaScript code? 有没有办法动态地使用端口或完整路径通过JavaScript代码获取React Native packager ip- jsCodeLocation It can be localhost:8081 or 192.168.0.1.xip.io:8081 . 它可以是localhost:8081192.168.0.1.xip.io:8081 It depends on the running device - simulator or real device 这取决于运行设备 - 模拟器或真实设备

I found NativeModules in ReactNative source code and discover scriptURL here: 在ReactNative源代码中找到了NativeModules并在这里发现了scriptURL:

import { NativeModules } from 'react-native';

...

const scriptURL = NativeModules.SourceCode.scriptURL;
const address = scriptURL.split('://')[1].split('/')[0];
const hostname = address.split(':')[0];
const port = address.split(':')[1];

It works on simulator and device! 它适用于模拟器和设备!

import { NativeModules } from 'react-native';
import url from 'url';

const { hostname } = url.parse(NativeModules.SourceCode.scriptURL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM