Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiomcosta/79f4a4bfb62317d111aad80fac654fae to your computer and use it in GitHub Desktop.
Save fabiomcosta/79f4a4bfb62317d111aad80fac654fae to your computer and use it in GitHub Desktop.
const App: () => React$Node = (props) => {
const [ players, setPlayers ] = useState([]);
useEffect(() => {
AsyncStorage.getItem('@maddness_user').then((userData) => {
getNearbyPlayers().then(performStompConnect);
}
});
}, []);
const getNearbyPlayers = () => {
return fetch(`${url}/v1/users`).then((data) => data.json()).then((json) => {
if (json && json['users']) {
setPlayers(json['users']);
}
}).catch(e => {
console.log(e);
});
}
const performStompConnect = (user) => {
client.onConnect = (frame) => {
console.log('STOMP: Got connect: ' + JSON.stringify(frame));
let stompTopics = [ `/exchange/users/${user['username']}`, '/exchange/global/global' ];
for (let i = 0; i < stompTopics.length; i++) {
client.subscribe(stompTopics[i], function(message) { handleMessage(message, user) });
}
};
}
const handleMessage = useCallback((msg, user) => {
// >>> 'players' here is EMPTY.. even though in the render prior to this I saw that it had 4 players <<
}, [players]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment