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

iOS: No client found with id X #64

Closed deiv23 closed 4 years ago

deiv23 commented 4 years ago

Description

I have problems on IOS, when I try to reuse the socket I receive: Error in connect() function no client found with id X

Dependencies

react-native 0.61.5 react-native-tcp-socket 3.2.4

Steps to reproduce the behavior:

calling a function like this twice.

function send()
{
         var options = {port:4444, host: x.x.x.x, reuseAddress:true, localAdress: y.y.y.y, localPort:4445}

         var client = TcpSocket.createConnection(options);
         client.on('error', function(error) {
             console.log(error)
         });
         client.on('data', function(data) {
             client.destroy();
         });
         client.write(JSON.stringify(jsArray));
}

Current behavior

if I execute the send() function once, everything works ok second time gives the error: Error in connect() function no client found with id X

In android works ok every time.

Expected behavior

work ok on second try

Rapsssito commented 4 years ago

@deiv23, you are calling the socket before it has been created. You have to use the callback provided by TcpSocket.createConnection():

var options = {port:4444, host: x.x.x.x, reuseAddress:true, localAdress: y.y.y.y, localPort:4445}
// TcpSocket.createConnection is async
var client = TcpSocket.createConnection(options, () => {
    client.write(JSON.stringify(jsArray));
});
client.on('error', function(error) {
     console.log(error)
});
client.on('data', function(data) {
    client.destroy();
});

I will update the docs to make it clear.

deiv23 commented 4 years ago

@Rapsssito thanks for the reply. Im trying to implement but that callback doesn't get called, I don't know why

Rapsssito commented 4 years ago

@deiv23, you can take a look at the example code for a more exhaustive example.

deiv23 commented 4 years ago

@Rapsssito I have the callback working, but second time I execute the function send, the createConnection callback isn't called. No errors shown. The server is ready to respond, I tested with an external tool after the first send, and the server is ready.

Rapsssito commented 4 years ago

@deiv23, it might be related to a bad config on your server or a wrong logic on your client's code. I cannot help you without any more information.

deiv23 commented 4 years ago

Hi, i tried with react-native-tcp and now its working fine. Thanks for your help anyway

El El mié, 8 jul 2020 a las 13:08, Rodrigo Martin notifications@github.com escribió:

@deiv23 https://github.com/deiv23, it might be related to a bad config on your server or a wrong logic on your client's code. I cannot help you without any more information.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Rapsssito/react-native-tcp-socket/issues/64#issuecomment-655450372, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKXLBG77BRTHK7N5KZB5NRDR2RHSNANCNFSM4ORVXMOQ .