Skip to content

Got [Error: AMQJS0011E Invalid state already connected.] when change the Screen in react native app. #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
momolly1024 opened this issue Oct 28, 2022 · 1 comment

Comments

@momolly1024
Copy link

momolly1024 commented Oct 28, 2022

Hi,

env: "react-native": "0.69.6",
version: "paho-mqtt": "^1.1.0",

I wrote the code like this :

 function onMessage(message) {
        let data = JSON.parse(message.payloadString)
        console.log(data)
     }
function onConnectionLost(responseObject) {
        if (responseObject.errorCode !== 0) {
            console.log('onConnectionLost:' + responseObject.errorMessage)
        }
    }

useEffect(() => {
        if (something) {
            try {
                client.connect({
                    onSuccess: () => {
                        console.log('Connected!')
                        client.subscribe(`device/default`)
                        client.onConnectionLost = onConnectionLost
                        client.onMessageArrived = onMessage
                    },
                    onFailure: () => {
                        console.log('Failed to connect!')
                    },
                })
            } catch (error) {
                console.log(error)
            }
        }
    }, [something])

It works but when I change the screen(react-navigation) I will got this error:
AMQJS0011E Invalid state already connected.
and nothing gets.

( Only when I reload the expo can get data again.)

Is there anything I miss?

Thank you!

image

@avilgrimaldy
Copy link

That's a sign that it has been connected and you called it again, aka you called connect more than once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants