Connection between two emulators — The confusing ride
“It’s one of those days when you don’t wanna wake up” said the song, well actually I had fever for the past 3 days and I can barely take a look at a bright screen (thank you missing dark mode on Medium) and still I had to figure out how to connect several android clients to a websocket that was running not on my host machine but in an emulator
Connecting an Android app to a Websocket on host machine
Dependencies:
- brew reinstall websocat
- org.java-websocket:Java-WebSocket
Reverse port forwarding:
To connect the guest android emulator I had to map a port from the emulator to the host machine.
Host web socket server running on port 8888, and guest mapped to 8888
adb reverse tcp:8888 tcp:8888
Then start a server on the local host
websocat -s 8888
Test the connection by opening the application and registering the client.
Connecting an echo Websocket on an emulator
Dependencies:
- brew reinstall websocat
- Android application running a websocket
Emulator port forwarding from host 8087 to emulator server 8888
adb forward tcp:8887 tcp:8888
Websocket connect to the server
websocat ws://localhost:8888
Connecting a client running on Emulator 5554 to server running on Emulator 5556
Now that we have made a client connect to an emulator, (see point above). Let’s see what happens if we try to use an emulator for that matter, it’s a multi-client server so we are good to go.
Dependencies:
- Enough RAM to handle multiple emulators.
Recommendation: use two old emulators on arm chipset on top of an Apple Silicon chipset that support natively the same instructions. Either way the RAM is always good to ask for more, so use this experiment to upgrade your Personal Computer haha
Start second emulator, then connect to ws://10.0.2.2:888 same as if you were connecting from the websocat terminal application.
Voilà! It just works.
Hopefully this helped someone out there, thanks for reading. Later.