Closed Nikotecnology closed 3 years ago
@Nikotecnology, take a look at #27, it might be the same issue.
No, I see that but it didn't resolve the problem
@Nikotecnology, what RN version are you using? Are you on iOS or Android? How are you importing the library?
The latest version, I downloaded with "npm i react-native-tcp-socket" I'm using expo cli with app on iOS
@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.
Ok, ill'try but I see more people that having this problem ,not with expo
@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.
Ok for that it's ok I will eject it and try
I removed expo and it seems working, but i cant close the connection externaly the function for connecting, how i can do?
@Nikotecnology, I don't understand your problem, could you elaborate?
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?
@Nikotecnology, take a look at the docs. You might want to use the destroy()
method.
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
@Nikotecnology, if you destroy the socket you must create a new socket to reopen the connection.
there isn't a way to close and reopen it without destroyng it
@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.
ok thanks i will close the issue
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?