Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
303 stars 81 forks source link

Socket remains open after reloading in react-native #177

Closed gtbX closed 11 months ago

gtbX commented 1 year ago

Description

Client sockets seem to remain open when refreshing the page, with no apparent way to close it.

This is an issue for us since the server we're connecting to only accepts a single client at a time (it's a network printer).

Steps to reproduce

Our code looks something like this:

import { connect } from 'net';
import React, { useState } from 'react';

export default function App() {

  const [socket, setSocket] = useState();

  const onConnectClicked = () => {
    setSocket(connect({host, port}, () => {
      socket.on('close', setSocket(null));
    }));
  };

  const onDisconnectClicked = () => socket.end();

  return (...);
}

Current behavior

With this setup, we can connect/disconnect from the socket using buttons in the UI. But if you force a refresh (e.g. hitting 'R' in the react-native console, "Reload App" in the debugger, or modifying some source files with Fast Refresh) while the socket is open, it remains open with no way to access it anymore. Verified using Wireshark that the connection isn't closed. Reload in this manner doesn't seem to trigger any onBeforeUnload hooks either, so I don't think it could be cleaned up that way.

Expected behavior

Any existing open sockets automatically close on page unload/refresh.

Relevant information

OS Android
react-native 0.71.3
react-native-tcp-socket 6.0.6
github-actions[bot] commented 11 months ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.