react-native-bluetooth-cross-platform icon indicating copy to clipboard operation
react-native-bluetooth-cross-platform copied to clipboard

fix for NSInternalInconsistencyException - caused by nil bridge in TransportHandler

Open joshbalfour opened this issue 7 years ago • 5 comments
trafficstars

Love the lib! Building a multiplayer game and it works well 😃

When using the library I got this error (#30):

NSInternalInconsistencyException. This is probably because you've explicitly synthesized the bridge in NetworkManager, even though it's inherited from RCTEventEmitter.

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 😊

joshbalfour avatar Mar 30 '18 11:03 joshbalfour

@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 avatar Mar 30 '18 16:03 michaeldanielbell

@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;
		}
	}
})

joshbalfour avatar Mar 31 '18 11:03 joshbalfour

👍

michaeldanielbell avatar Mar 31 '18 11:03 michaeldanielbell

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 avatar May 02 '18 03:05 alexkendall

@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...

vimalathiyagu avatar Dec 19 '18 19:12 vimalathiyagu