react-native-tcp-socket: Connection error never emitted on iOS

Description

Hello!!

I’m trying to create a tcp connection to an incorrect host (the ip is unreachable).

Here is my code:


...
private _device = null

private async openConnection(
        host,
        dataListenerCallback,
    ): Promise<void> {
        if (this._device) {
              this._device.destroy();
        }

        return new Promise((resolve, reject) => {
            this._device = net.createConnection({
                port: 53333,
                host,
                timeout: 1000,
            }, (socket) => {
                console.log('connected');
                resolve();
            });

            this._device.on('connect', () => {
                console.log('connect');
            });

            this._device.on('connection', () => {
                console.log('connnection');
            });

            this._device.on('error', (error) => {
                console.log('Error', error);
                reject();
            });

            this._device.on('data', dataListener);

            this._device.on('close', () => {
                console.log('Connection closed!');
            });
        });

Current behavior

this function never rejects or resolves.

Expected behavior

I expected to catch a connection error in the error event listener.

Relevant information

| react-native | 0.61.5 | | react-native-tcp-socket | 3.7.1 |

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 33 (15 by maintainers)

Most upvoted comments

@mi-mazouz, I am glad it worked! If you agree, I think we can close this issue now. It has been a difficult problem to tackle.

@itzsankar, this issue has +30 comments right now. Could you please create a new issue with your problem, the devices with issues and a reproducible code?

I updated the package to the latest version and tested this code:

import React from 'react';
import {
    StyleSheet,
    Text,
    View,
} from 'react-native';
import TcpSocket from 'react-native-tcp-socket';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.updateChatter = this.updateChatter.bind(this);
        this.state = { chatter: ['hello'] };
    }

    updateChatter(msg) {
        this.setState({
            chatter: this.state.chatter.concat([msg]),
        });
    }

    componentDidMount() {
        const serverPort = 53333;
        const serverHost = '192.1.1.3';
        let client;

        client = TcpSocket.createConnection({
            port: serverPort,
            host: serverHost,
        }, (address) => {
            this.updateChatter(`opened client on ${JSON.stringify(address)}`);
            client.write('Hello, server! Love, Client.');
        });

        client.on('connect', () => {
            this.updateChatter('Client connect');
            console.log('CONNECT');
        });

        client.on('timeout', () => {
            this.updateChatter('Client timeout');
            console.log('TIMEOUT');
        });

        client.on('data', (data) => {
            console.log('DATA');
            this.updateChatter(`Client Received: ${data}`);
        });

        client.on('error', (error) => {
            console.log('ERROR');
            this.updateChatter(`client error ${error}`);
        });

        client.on('close', () => {
            console.log('CLOSE');
            this.updateChatter('client close');
        });

        this.client = client;
    }

    componentWillUnmount() {
        this.client = null;
    }

    render() {
        return (
            <View style={styles.container}>
                {this.state.chatter.map((msg, index) => (
                    <Text key={index} style={styles.welcome}>
                        {msg}
                    </Text>
                ))}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'red',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        color: 'black',
        margin: 10,
    },
});

export default App;

and after 1 min and 15secs the error and close events were triggered so that’s a good new.

@mi-mazouz, I have not been able to figure it out yet. I have came into the conclusion that it might be related to GCDAsyncSocket inner workings. I am still working on it. If you find anything, please let me know.

@mi-mazouz, glad it works with setTimeout()! I am not able to reproduce the issue, the device on your 192.168.1.1 may be accepting the TCP connection and keeping it on hold forever (that’s why you don’t get any error).

I will be releasing a setTimeout() update soon (#56), it should work in all scenarios.

@Rapsssito sure, I have created a new issue #106 we can track from there , let me know if you need any details

my bad this_.device.removeListener(‘timeout’) in createConnection callback did the job!

@ewc003, could you create a new issue with your specific problem?

@mi-mazouz, I am able to reproduce the bug on iOS. Working on it.