-
Notifications
You must be signed in to change notification settings - Fork 157
Open
Labels
Description
Description
I have an app that sends out a broadcast message to companion server apps on the same network. The servers should be able to see this message then send a message back to the app directly with unicast. The whole system works when in debug mode but once the I build the release APK the app no longer receives the messages from the servers.
Edit: Experiencing the issue on both iOS and Android. Debug works as expected, Release does not.
Things we know:
- The app still sends the broadcast message in both versions
- The message from the server is being sent.
- Socket is being bound in release
- Testing on device
Release Permissions same as Debug
Expected behaviour (Debug):
Shows binding and most recent message
Release behaviour:
No message
Wireshark shows message is sent
Code
import dgram from 'react-native-udp';
import { NetworkInfo } from 'react-native-network-info';
import { useEffect, useRef, useState } from 'react';
import { useID } from '../Context/IDContext';
import { ServerData, useServerData } from '../Context/ServerDataContext';
import { useSocket } from '../Context/SocketContext';
import { AppState, Platform, Text } from 'react-native';
import UdpSocket from 'react-native-udp/lib/types/UdpSocket';
const bindingPort = 6537;
const broadcastPort = 6536;
var broadcastAddress = "";
const broadcastFrequency = 10000;
var udpSocket:UdpSocket;
var detailText = "";
const getBroadcastAddress = async(): Promise<String> => {
//removed for space
};
const bindUDPSocket = ()=>{
udpSocket = dgram.createSocket({type:"udp4",reusePort:true,debug:true})
getBroadcastAddress().then(bAdd =>{broadcastAddress = bAdd.toString()});
udpSocket.once('listening',()=>{
if(Platform.OS != "android"){
udpSocket.setBroadcast(true);
}
detailText = `UDP BOUND to ${udpSocket.address().address}:${udpSocket.address().port}`;
console.log(`UDP BOUND to ${udpSocket.address().address}:${udpSocket.address().port}`);
});
udpSocket.on('error',(err:any)=>{
console.log(err);
udpSocket.close();
});
udpSocket.bind(bindingPort);
};
bindUDPSocket();
export const Discovery = () => {
const idContext = useID();
const serverDataContext = useServerData();
const socketContext = useSocket();
const changeTimer = useRef<NodeJS.Timeout>(undefined);
const broadcastTimer = useRef<NodeJS.Timeout>(undefined);
const [lastUPDMessage, setLastUDPMessage] = useState("");
useEffect(()=>{
clearInterval(broadcastTimer.current);
clearTimeout(changeTimer.current);
changeTimer.current = setTimeout(buildMessage,1000);
broadcastTimer.current = setInterval(buildMessage,broadcastFrequency);
return () => {
clearInterval(broadcastTimer.current);
}
},[idContext?.uuid,idContext?.provenceID]);
const handleUDPMessages = (message:any,remoteInfo:any)=>{
try{
const messageObj = JSON.parse(message.toString());
console.log(messageObj); //<-- Logging message
if('eventType' in messageObj){
switch(messageObj.eventType){
case 'socket-connection-request':
const serverData = {address:remoteInfo.address,port:messageObj.port} as ServerData;
console.log(serverData);
serverDataContext?.dispatch({type:'set',serverData:serverData});
socketContext?.dispatch({type:'connect',serverData:serverData});
break;
}
}
setLastUDPMessage(message.toString()); //<-- Drawing to screen
}catch(err){
console.log(err);
}
};
useEffect(()=>{
udpSocket.on('message',handleUDPMessages); //<--- getting message
AppState.addEventListener('change',(state)=>{
if(state == "active"){
udpSocket.close();
bindUDPSocket();
}
});
return()=>{
udpSocket.off('message',handleUDPMessages);
};
},[]);
function buildMessage (){
console.log(idContext?.provenceID,broadcastAddress);
const message = JSON.stringify({uuid: idContext?.uuid, provenceID: idContext?.provenceID});
udpSocket.send(message,undefined,undefined,broadcastPort,broadcastAddress);
};
return(<>
<Text style={{color:"white"}}>{`${detailText},${lastUPDMessage}`}</Text>
</>);
};Relevant information
| OS | Android / iOS |
| react-native | 0.79.2 |
| react-native-udp | 4.1.7 |
Reactions are currently unavailable