AMQJS0011E 未连接的状态无效

AMQJS0011E Invalid state not connected

我正在尝试在覆盆子槽 paho 上的 MQTT 代理上发布消息。 我用 visual studio 2015(在 windows 10)构建了一个“应用程序”,我正在使用 ripple 模拟器来测试它,但我总是得到这个错误:

AMQJS0011E Invalid state not connected.

我还尝试导出文件并在 linux 系统上使用 firefox 作为常规网页打开它们,但我遇到了同样的错误,所以我认为这不是什么 windows相关。

用按钮触发的函数是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);
}

您试图在连接打开之前发送内容。

这应该表现得更好并确保一切按顺序进行

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