Forked from wedgemartin/gist:6c2c2d4c6471ac77ecbe691dcbf6bb42
Last active
October 7, 2021 22:23
-
-
Save fabiomcosta/79f4a4bfb62317d111aad80fac654fae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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