Open joshbalfour opened 6 years ago
@joshbalfour
Nice job, out of interest, once devices are connected in your multiplayer game. How are you communicating between devices? Using the BluetoothCP.sendMessage
? Or an alternative way?
@michaeldanielbell Thanks!
I am, a simplified version of how i'm doing this is:
const {
getConnectedPeers,
addReceivedMessageListener,
sendMessage,
} = require('react-native-bluetooth-cross-platform')
import newGameStateHandler from './newGameState'
import peerIdentifiedHandler from './peerIdentifiedHandler'
export const broadcast = ({ type, data }) => {
getConnectedPeers(peers => {
peers.forEach(peer => {
console.log('sending', { type, data })
sendMessage(JSON.stringify({ type, data }), peer.id)
})
})
}
export const emit = ({ type, data, id }) => {
console.log(`emitting to ${id}`, { type, data })
sendMessage(JSON.stringify({ type, data }), peer.id)
}
addReceivedMessageListener(({ message, ...peer }) => {
let msg
try {
msg = JSON.parse(message)
} catch (e) {
console.error('non json parsable message recieved')
}
if (msg) {
const { type, data } = msg
if (!type) {
console.error('message without type recieved')
}
switch (type) {
case 'new-game-state':
newGameStateHandler({ gameState: data, peer })
break;
case 'identify':
peerIdentifiedHandler({ peer, identity: data })
break;
default:
break;
}
}
})
👍
Hi @joshbalfour. Thanks for the changes! Sorry I haven't been able to get onto this sooner. Planning on testing these and merging them in.
@alexkendall : it would be great if you could merge these changes into the lib if all well. Since we are using this library and the issue is causing crashes consistently...
Love the lib! Building a multiplayer game and it works well 😃
When using the library I got this error (#30):
To fix this I refactored out the RCTEventEmitter into a separate object which is instantiated on app start, which TransportHandler can then use when it needs.
I followed this guide https://gist.github.com/brennanMKE/1ebba84a0fd7c2e8b481e4f8a5349b99 I'm not a swift programmer by any stretch, so there may be a better way of doing this but this works 😊