Skip to content

Commit dd8f838

Browse files
authored
Merge pull request #59 from jhen0409/patch-1
Get correctly localhost for Android emulator
2 parents 8fe3db7 + c72d0b7 commit dd8f838

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,6 @@ You can also register steps if you're timing a sequence.
202202

203203
# Tips
204204

205-
##### Using With Android
206-
207-
If you're using an Android sim and it's running 5.x or higher, you can use `adb` to port forward
208-
the right TCP port to the `reactotron` server.
209-
210-
`$ANDROID_HOME/platform-tools/adb reverse tcp:3334 tcp:3334`
211-
212205
##### Using On A Device
213206

214207
When you initialize the `reactotron` you can tell it the server location when you connect:

client/client.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ client.onCommand('devMenu.reload', (action, client) => {
4646
// devMenu && devMenu.reload()
4747
})
4848

49+
/*
50+
* Get React Native server IP if hostname is `localhost`
51+
* On Android emulator, the IP of host is `10.0.2.2` (Genymotion: 10.0.3.2)
52+
*/
53+
function getHost(hostname) {
54+
if (
55+
(hostname === 'localhost' || hostname === '127.0.0.1') &&
56+
typeof window !== 'undefined' &&
57+
window.__fbBatchedBridge &&
58+
window.__fbBatchedBridge.RemoteModules &&
59+
window.__fbBatchedBridge.RemoteModules.AndroidConstants
60+
) {
61+
const {
62+
ServerHost = hostname
63+
} = window.__fbBatchedBridge.RemoteModules.AndroidConstants;
64+
return ServerHost.split(':')[0];
65+
}
66+
67+
return hostname;
68+
}
69+
4970
/**
5071
Connect to the server.
5172
@param userConfigurations Client configuration for connecting to Reactotron
@@ -66,7 +87,8 @@ client.connect = (userConfigurations = {}) => {
6687
// merge user input with defaults
6788
const config = {
6889
...defaults,
69-
...userConfigurations
90+
...userConfigurations,
91+
server: getHost(userConfigurations.server || defaults.server)
7092
}
7193

7294
// keep track for all ops

0 commit comments

Comments
 (0)