Sifir-io / react-native-tor

Tor Daemon and Socks5 client for React Native iOS and Android projects!
MIT License
101 stars 15 forks source link

Socket usage example results in "no listeners registered" error: React-Native- iOS #23

Closed skyworms closed 3 years ago

skyworms commented 3 years ago

After following the installation instructions, attempting to use the Socket Usage Example in React Native on iOS results in the following error, Sending 'torTcpStreamData' with no listeners registered.

Is there a step I may be missing?

gabidi commented 3 years ago

Hard to tell without seeing your code. The example app included in the repo: https://github.com/Sifir-io/react-native-tor/blob/main/example/src/App.tsx#L79-L100 Includes an example use of the socket module. Maybe that helps, if not kindly repost with a code snippet. Thanks!

skyworms commented 3 years ago

Of course. Thanks for passing along the example. That example snippet seems to work just fine for me when I run the simulator. The sample code from the README.md that I was receiving the error with is:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useEffect} from 'react';
import {SafeAreaView, StatusBar} from 'react-native';
import TorBridge from 'react-native-tor';
const tor = TorBridge();

const _init = async () => {
  try {
    await tor.startIfNotStarted();
    const target = 'kciybn4d4vuqvobdl2kdp3r2rudqbqvsymqwg4jomzft6m6gaibaf6yd.onion:50001';
    const conn = await tor.createTcpConnection({ target }, (data, err) => {
      if(err){
        console.error('error sending msg',err);
        return
      }
      console.log('recieved tcp msg', data);
    } );

    try {
      await conn.write(`{ "id": 1, "method": "blockchain.scripthash.get_balance", "params": ["716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c"] }\n`);
      await conn.write(`{ "id": 2, "method": "blockchain.scripthash.get_balance", "params": ["716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c"] }\n`);
      // ... moar writes
    } catch (err) {
      console.error('Error SendingTcpMSg', err);
    }
    await conn.close();
  } catch (e) {
    console.log(e);
  }
};

const App: () => React$Node = () => {
  useEffect(() => {
    _init();
  }, []);
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView />
    </>
  );
};
export default App;
gabidi commented 3 years ago

Yeah that example should be updated. To clarify, calling:

await conn.close();

Closes the TCP connection and removes any attached lsners. So :

try {
      await conn.write(`{ "id": 1, "method": "blockchain.scripthash.get_balance", "params": ["716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c"] }\n`);
      await conn.write(`{ "id": 2, "method": "blockchain.scripthash.get_balance", "params": ["716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c"] }\n`);
      // ... moar writes
    } catch (err) {
      console.error('Error SendingTcpMSg', err);
    }
    await conn.close();

Will :

  1. Send Two Writes.
  2. Close the connection (ergo sum remove the lsners)
  3. Then some time later your data response comes through but lsners have been removed by closing the connection, thus the error "no listeners registered" being thrown.

Makes sense ?

skyworms commented 3 years ago

Ooooooh I see. Thanks for the explanation! That makes sense.