Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
315 stars 80 forks source link

TcpSocket can not connect when used from library function #198

Open MrEMU opened 1 month ago

MrEMU commented 1 month ago

Description

I am currently working on a conversion of the node-thermal-printer library into a React Native library. For this, I need to replace some node modules with fitting React Native counterparts. I want to use the react-native-tcp-socket library as a substitute for the net library.

The problem that has arisen is, that the TcpSocket would not get a connection, when I try to use it in the library build by react-native-builder-bob. The same code will successfully execute, if put in a function directly inside the app, though.

In my case, I'm using a printer that is connected in the same network as my PC.

I have built an example as minimal as possible for this problem, so it can be easily reproduced.

Steps to reproduce

Steps to reproduce the behavior:

  1. Clone my minimal example repository
  2. Go inside the repository: cd awesome-library
  3. Install and build package: npm i
  4. Go inside the example folder: cd example
  5. Install the example App: npm i
  6. Change the host and port inside the App.tsx to a valid target to connect to.
  7. Go inside the android folder: cd android
  8. Build the project with Gradle: .\gradlew clean
  9. Go back into the example folder: cd ..
  10. Start an android emulator
  11. Start the app in the console: npx react-native start
  12. Press a in the console to install the app on the emulator
  13. Test the buttons and check the output in the console
  14. Direct button will log Connected! indicating a successful connection
  15. Library button will log an Error Printer network connection timeout. indicating a timeout

Known issue

When pressing the library button and then the direct button afterward, an error is displayed the first time: ERROR Printer network connection error: connect() called twice with the same id.

Current behavior

The TcpSocket.createConnection() method times out, every time it is used from inside a self build library, when the same connection works from inside the app directly.

Expected behavior

The TcpSocket.createConnection() method should behave the same when used inside a self build library or when used directly in the app.

Relevant information

OS Windows/Android
react-native 0.74.3
react-native-tcp-socket ^6.2.0