Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AMQJS0011E Invalid state not connected

I'm trying to publish a message on a MQTT Broker on a raspberry trough paho. I've built an "app" with visual studio 2015 (on windows 10) and I'm using the ripple simulator to test it but I always get this error:

AMQJS0011E Invalid state not connected.

I also tried to export the files and to open them as regular webpages with firefox on a linux system and I get the same kind of error so I don't think is something windows related.

The function that gets triggered with a button is playCanzone()

function playCanzone() {
console.log("play premuto");
mqttHost = '192.168.9.184';
topic = 'testTopic';
client = new Paho.MQTT.Client(mqttHost, 8080, "myclientid_" + parseInt(Math.random() * 100, 10));
onConnect();//publish('mEssaggio', 'testtopic/bar', 2);
}

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({ onSuccess: onConnect });

// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe(topic);
message = new Paho.MQTT.Message("Hello");
message.destinationName = topic;
client.send(message);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:" + responseObject.errorMessage);
}
}

// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
}
like image 883
elisa tramonti Avatar asked Oct 26 '25 00:10

elisa tramonti


1 Answers

Your trying to send things before the connection is open.

This should behave better and ensure everything happens in order

var client; topic;

function playCanzone() {
  console.log("play premuto");
  var mqttHost = '192.168.9.184';
  topic = 'testTopic';
  client = new Paho.MQTT.Client(mqttHost, 8080, "myclientid_" + parseInt(Math.random() * 100, 10));
  // set callback handlers
  client.onConnectionLost = onConnectionLost;
  client.onMessageArrived = onMessageArrived;

  // connect the client
  client.connect({ onSuccess: onConnect });
}

// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe(topic);
  var message = new Paho.MQTT.Message("Hello");
  message.destinationName = topic;
  client.send(message);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:" + responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:" + message.payloadString);
}
like image 113
hardillb Avatar answered Oct 28 '25 12:10

hardillb