1
votes

I am using SSDP search message to discover devices with connected same network but when i tried to call setState hooks inside client.on function I only get one device informations.

I initialized my state value in this way

const [deviceList, setDeviceList] = useState([]);

And create a function for the client to add deviceList as it is found

const getAllDevices = () => {

var Client = require('react-native-ssdp-remote').Client,
  client = new Client();

client.search('urn:dial-multiscreen-org:service:dial:1');

client.on('response', function (headers) {
  const url = new URL(headers.LOCATION);
  if (url != null) {
    if (!deviceList.includes(url)) {
      setDeviceList([...deviceList, url]);
    }
  }
});
};

and called this function inside useEffect

  useEffect(() => {
  getAllDevices();
  }, []);

There are 4 devices connected to same network and it goes into setDeviceList process 4 times but i can only get one device. Could you please support.

Thanks.

Your code seems to be ok. It looks like the problem that you are face on are related with the RN library called "react-native-ssdp" and not related with the useState hook. Do you already checked the library specs in order to get the correct way to obtain all client list?Ali Briceño