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

Close connection outside the function #92

Closed Nikotecnology closed 3 years ago

Nikotecnology commented 3 years ago

Hi, i was tryng to install this repo but i can't start the application because of this error: "TypeError: undefined is not an object (evaluating '_reactNativeTcpSocket.default.createConnection')". Now i reinstalled the library and now it says this error: "Native module cannot be null" Someone knows how to resolve this?

Rapsssito commented 3 years ago

@Nikotecnology, take a look at #27, it might be the same issue.

Nikotecnology commented 3 years ago

No, I see that but it didn't resolve the problem

Rapsssito commented 3 years ago

@Nikotecnology, what RN version are you using? Are you on iOS or Android? How are you importing the library?

Nikotecnology commented 3 years ago

The latest version, I downloaded with "npm i react-native-tcp-socket" I'm using expo cli with app on iOS

Rapsssito commented 3 years ago

@Nikotecnology, I am sorry I cannot help you with Expo. However, you might need to eject your project first in order to get custom modules to work.

Nikotecnology commented 3 years ago

Ok, ill'try but I see more people that having this problem ,not with expo

Rapsssito commented 3 years ago

@Nikotecnology it is a common issue among custom modules. React Native automatic linking sometimes fails, the XCode cache is not correctly cleared, the module download is corrupt, metro cache is not correctly cleared, etc.

Nikotecnology commented 3 years ago

Ok for that it's ok I will eject it and try

Nikotecnology commented 3 years ago

I removed expo and it seems working, but i cant close the connection externaly the function for connecting, how i can do?

Rapsssito commented 3 years ago

@Nikotecnology, I don't understand your problem, could you elaborate?

Nikotecnology commented 3 years ago

I created a function(_connect) and when the user clicks the button the connection stabilish, i want to do the disconect button and the reconnect button how i can do?

Rapsssito commented 3 years ago

@Nikotecnology, take a look at the docs. You might want to use the destroy() method.

Nikotecnology commented 3 years ago

no, i didn't explain that this is my code:

import React from 'react';
import {
  SafeAreaView,
  TouchableOpacity,
  Text,
  StyleSheet,
  Alert,
} from 'react-native';
import Clipboard from '@react-native-community/clipboard';
import TcpSocket from 'react-native-tcp-socket';
let that;
let connected = true;

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

const client = TcpSocket.createConnection(
  {
    host: '192.168.1.202',
    port: 49155,
  },
  () => {},
);

export default class App extends React.Component {
  constructor(props) {
    super(props);
    that = this;
    this._connect = this._connect.bind(this);
    this.state = {
      textValue: 'Change me',
    };
  }

  _connect = () => {
    if (!connected) {
      client.connect();
    }
    client.on('data', function (data) {
      console.log(String.fromCharCode.apply(null, data));
      that.setState({
        textValue: String.fromCharCode.apply(null, data),
      });
      setInterval(() => {
        Clipboard.setString(String.fromCharCode.apply(null, data));
      }, 1500);
    });

    client.on('error', function (error) {
      console.log(error);
    });

    client.on('close', function () {
      console.log('Connection closed!');
      that.setState({
        textValue: ' ',
      });
    });
  };

  _destroy = () => {
    client.destroy();
    connected = false;
  };

  render() {
    return (
      <>
        <SafeAreaView style={styles.container}>
          <TouchableOpacity
            style={{
              alignItems: 'center',
              color: 'black',
              fontFamily: 'SemiBold',
              marginTop: 40,
            }}
            onPress={this._connect}>
            <Text>Connect</Text>
          </TouchableOpacity>
          <Text style={styles.text}>{this.state.textValue}</Text>
          <TouchableOpacity
            style={{
              alignItems: 'center',
              color: 'black',
              fontFamily: 'SemiBold',
              marginTop: 80,
            }}
            onPress={this._destroy}>
            <Text>Disconect</Text>
          </TouchableOpacity>
        </SafeAreaView>
      </>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#61dafb',
  },
  text: {
    justifyContent: 'center',
    alignItems: 'center',
  },
});

It closes the connection but i can't reopen it

Rapsssito commented 3 years ago

@Nikotecnology, if you destroy the socket you must create a new socket to reopen the connection.

Nikotecnology commented 3 years ago

there isn't a way to close and reopen it without destroyng it

Rapsssito commented 3 years ago

@Nikotecnology, that is not how TCP connections work, it is a connection-oriented protocol. Take a look at the wikipedia article about TCP for more info.

Nikotecnology commented 3 years ago

ok thanks i will close the issue