Closed preduseldavid closed 4 years ago
@preduseldavid, thanks for your feedback, could you please provide your code, OS, RN version, library version, etc?
Code:
async _connectToServer() {
let options = {host: this._serverHost, port: this._serverPort};
let loading = true;
NetInfo.fetch().then(state => {
if (state.isConnected) {
let client = TcpSocket.createConnection(options, () => {
if (!this._toastInternetShow && this._reconnecting)
self.postMessage(
JSON.stringify({
event: 'toast_show',
data: {
text: 'Connected Location',
loading: false,
timeout: 3000,
},
}),
);
else global.threadService.threadEventSend({event: 'toast_hide'});
this._reconnecting = false;
this._toastInternetShow = true;
this._internetServiceEnabled = true;
/* Do we have the location available? */
let location = global.locationService.getLocation();
if (global.locationService.locationIsAvailable(location)) {
/* Register - send our location coords */
this.updateLocation(location);
}
this._queueSendingPacketResume();
});
client.on('data', data => {
if (loading) {
loading = false;
self.postMessage(
JSON.stringify({
event: 'groups_loading',
data: {loading: false},
}),
);
}
this._readEvent(data);
});
client.on('error', error => {
Reactotron.log(error);
this._client.destroy();
if (!this._reconnecting) {
this._reconnecting = true;
self.postMessage(
JSON.stringify({
event: 'toast_show',
data: {
text: 'Reconnecting Location',
loading: true,
timeout: 3000,
onerror: true,
},
}),
);
setTimeout(this._connectCallback, 1000);
}
});
client.on('close', () => {
this._client.destroy();
if (!this._reconnecting) {
this._reconnecting = true;
self.postMessage(
JSON.stringify({
event: 'toast_show',
data: {
text: 'Reconnecting Location',
loading: true,
timeout: 3000,
onclose: true,
},
}),
);
setTimeout(this._connectCallback, 1000);
}
});
this._client = client;
} else {
this._reconnecting = 1;
this._internetServiceEnabled = false;
if (this._toastInternetShow) {
this._toastInternetShow = false;
self.postMessage(
JSON.stringify({
event: 'toast_show',
data: {
text: 'You are Offline Location',
loading: false,
timeout: 3000,
nointernet: true,
},
}),
);
}
setTimeout(this._connectCallback, 2000);
}
});
}
OS: Android RN Version: 0.62 library version: "react-native-tcp-socket": "^3.6.0",
@preduseldavid, when does this error occur? Could you also share the full error trace?
@Rapsssito I don't have the full error trace, but will share it when the error occurs again. Sometimes it appears, and the most of the times it doesn't... I want to also let you know that the connection is closed sometimes with this error: java.net.SocketException: Socket is not connected (and I use the above code)
@preduseldavid, those kind of errors are probably caused by calling the socket before it has finished establishing the connection.
I see you declare this._client = client;
before the TcpSocket.createConnection
callback has been executed. This may lead to this kind of errors if you call this._client
quickly enough. Try to move this declaration (this._client = client;
) inside the callback:
let client = TcpSocket.createConnection(options, () => {
this._client = client;
if (!this._toastInternetShow && this._reconnecting)
...
});
I get this error and the socket connection is closed: Attempt to invoke virtual method 'void java.net.Socket.setReuseAddress(boolean)' on a null object reference