alexkendall / react-native-bluetooth-cross-platform

Cross-Platform Bluetooth & WiFi React Native Module. Powered by underdark http://underdark.io/.
179 stars 35 forks source link

fix for NSInternalInconsistencyException - caused by nil bridge in TransportHandler #44

Open joshbalfour opened 6 years ago

joshbalfour commented 6 years ago

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 😊

michaeldanielbell commented 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?

joshbalfour commented 6 years ago

@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;
        }
    }
})
michaeldanielbell commented 6 years ago

👍

alexkendall commented 6 years ago

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.

vimalathiyagu commented 5 years ago

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