I have a Worklight project that uses Paho MQTT Javascript 'mqttws31.js'.
In the simulator everything works fine, but when connecting from a device it always gives me a timeout.
When I connect to the broker through the app called 'MyMQTT' on the device it connects fine. I can't get it to work with Javascript.
I tested if websockets are supported with this code - Link
The webview in the application supports websockets, so that could not be the problem.
I tried several connection options, but it always times out.
Are there some Android permissions I have to add?
I added one of the examples I tried below.
The broker that I use is HiveMQ and I enabled websocket support on port 8000.
I also tried to connect to a public broker - test.mosquitto.org", 80
But that gives me the same results (works in simulator, not on device).
Please help me!
I know that the Cordova plugin would be better than the Javascript version, but I don't find the Cordove MQTT plugin for Cordova 3.1. That could help me out to.
var client = new Messaging.Client("192.168.137.2", 8000, "prototype2");
$(function() {
$("#publishBtn").click(function() {
publish("test", "prototype2/testpublish", 1);
});
var options = {
// connection attempt timeout in seconds
timeout : 5,
// Gets Called if the connection has successfully been established
onSuccess : function() {
$("#console").append(
'<br/>' + "Connected to MQTT Broker over Websockets.");
// client.subscribe("testtopic", {qos: 2});
client.subscribe("prototype3/testpublish");
},
// Gets Called if the connection could not be established
onFailure : function(message) {
$("#console").append(
'<br/>' + "Connection failed: " + message.errorMessage);
}
};
// Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function(message) {
var topic = message.destinationName;
var message = message.payloadString;
$("#console").append(
'<br/>' + 'Message arrived: Topic: ' + topic + ', Message: '
+ message);
};
// Attempt to connect
client.connect(options);
});