kirillzyusko / react-native-wifi-p2p

Library that provide access for working with wi-fi direct (p2p) module in android.
164 stars 32 forks source link

SendMessageTo - can't use MAC address and we don't have ip addresses #77

Open seba9999 opened 1 year ago

seba9999 commented 1 year ago

The list of peer only provide peers info with some MAC addresses ... But for now the sendMessage uses InetSocketAddress which can't use MAC addresses.

I'll maybe try to pass the ip from the client to the server at first message ( in order to be able to transfert message from group owner to clients )

Or we need some refactoring in the sendMessage function to add extra fields to store ip list of devices / peer in the group ...

kirillzyusko commented 1 year ago

I'll maybe try to pass the ip from the client to the server at first message ( in order to be able to transfert message from group owner to clients )

Hi @seba9999 Let me know, whether it works for you or not 👀

viniciuscb commented 1 year ago

Hi @seba9999 . I tried to find how can one know the ip addresses of other peers. What I've found is that people is sending a first message from clients (peers) to group owner to know the IP. After that, the communication continues normally.

I think that the README should be updated explaining this usage.

seba9999 commented 1 year ago

Yeah I've found thoses threads ... But I can't believe there's nothing newer than 10 years old !

Is it still the best ( right ) way of doing such "broadcast" ?

It would be better to implement it java side no ? I'm wondering

viniciuscb commented 1 year ago

Hi @seba9999 . The problem is that the information that we receive in react native is exactly the information that android gives us.

I've sent a PR now ( #80 ) that changes the receiveMessage method, and we also receive in react native the ip of the sender. In my application I am using like this:


  if (connectionInfo.groupFormed) {
    if (!connectionInfo.isGroupOwner) {
      sendMessage('it can be anything here');
    } else {
      receiveMessage({meta: true}).then(({fromAddress, message}) => {
        // now you have the IP address of the peer in fromAddress. You can store it
        // somewhere to use in the future: sendMessageTo(message, address)
      });
    }
  }

It seems to me that this is the best solution, as if the client tries to find its own IP address through other method, it will need to filter, because the device most surely have several: ipv4 and ipv6 (localhost ip, current network ip and the wifip2p ip). When I receive the message, in java, I get the information of the source ip in the client socket through which the message is coming. This is the IP of the sender in the wifip2p network.

viniciuscb commented 1 year ago

I've not tried to send a message to the broadcast address, though.

viniciuscb commented 1 year ago

as receiveMessage receives just one message, I'm implementing it the following way:


  const onReceiveMessage = () => {
    receiveMessage({meta: true})
      .then({ fromAddress, message } => {
        let receiveNextMessage = true;

        // Conditions: depending on the message received or other logic, will 
        // close the connection and set receiveNextMessage to false. 
        //     
        // Can call stopReceivingMessage() to close opened socket. I am calling 
        // on screen exit (useEffect return) just in case. This ensures the promise.resolve()
        // callback won't be called in another context, i.e. another screen....

        if (receiveNextMessage) {
          onReceiveMessage();
        }
      })
      .catch(err => {
        console.log(user.name + ':   Error while message receiving', err);
        onReceiveMessage();
      });
  };
Suyashbajpai27 commented 3 months ago

I have same issue that i am not able to send message because it is capturing the MAC address not IP address.