2016-03-18 2 views
0

Я пытаюсь опубликовать сообщение на брокером MQTT на малине, вызванной paho. Я построил «приложение» с визуальной студией 2015 (на окнах 10), и я использую имитатор пульсаций, чтобы проверить его, но я всегда получаю эту ошибку.AMQJS0011E Неправильное состояние не подключено

AMQJS0011E Недопустимое состояние не подключено.

Я также пытался экспортировать файлы и открывать их как обычные веб-страницы с firefox на linux sistem, и я получаю такую ​​же ошибку, поэтому я не думаю, что это связано с окнами.

Функция, которая получает срабатывает с помощью кнопки является 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); 
} 
+0

Почему вы называете OnConnect в функции playCanzone? – hardillb

+1

иначе как передать сообщение и тему? это не функция, которая фактически передает данные брокеру? –

ответ

2

Вы пытаетесь отправить вещи, прежде чем соединение открыто.

Это должно вести себя лучше, и убедиться, что все происходит в порядке

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

спасибо @hardillb, который решил это :) –

Смежные вопросы