我正在使用 Websockets 在三星 Gear S 上的 Tizen 中实现 MQTT 协议。虽然在 IDE 模拟器中一切正常,但在手表上运行它时,我总是收到 SecurityError: DOM Exception 18。
访问来源设置:
<access origin="http://broker.hivemq.com" subdomains="true"/>
<access origin="http://broker.hivemq.com:8000" subdomains="true"/>
<access origin="http://broker.hivemq.com:1883" subdomains="true"/>
<access origin="*" subdomains="true"></access>
<access origin="http://test.mosquitto.org" subdomains="true"/>
<access origin="http://test.mosquitto.org:1883" subdomains="true"/>
<access origin="http://test.mosquitto.org:8080" subdomains="true"/>
<tizen:content-security-policy>connect-src http://www.heartxxx.bt
 ... http://broker.hivemq.com;
</tizen:content-security-policy>
<tizen:privilege name="http://tizen.org/privilege/internet"/>
我错过了什么?感谢帮助!
克莱门斯
这里使用了 MQTT 代码。是使用 JavaScript 库 mqttws31.ja von IBM(基本上是 PAHO)
var client = null;
var topic = "sensor";
var qos = 2;
var options =
{
// connection attempt timeout in seconds
timeout : 3,
// Gets Called if the connection has successfully been established
onSuccess : function()
{
$("#mqttout").append("mqttconnect: Connected" + "<br />");
/*
* client.subscribe(topic, { qos : qos });
*
*/
client.subscribe("adl",
{
qos : qos
});
},
// Gets Called if the connection could not be established
onFailure : function(message)
{
$("#mqttout").append(
"mqttconnect: Connection failed: " + message.errorMessage
+ "<br />");
}
};
function mqttclear()
{
$("#mqttout").empty();
}
var isens = 0;
// Host: broker.hivemq.com
// Port: 1883
// Websocket Port: 8000
// Host: test.mosquitto.org
// Port: 1883
// Websocket Port: 8080
var mqttservers = [
{
"server" : "broker.hivemq.com",
"port" : 1883,
"webport" : 8000
},
{
"server" : "test.mosquitto.org",
"port" : 1883,
"webport" : 8080
},
{
"server" : "192.168.2.113",
"port" : 1883,
"webport" : 8000
}];
var mqttserver = "broker.hivemq.com";
var mqttwebport = 8000;
function setmqttserver(i)
{
mqttdisconnect();
mqttserver = mqttservers[i].server;
mqttwebport = mqttservers[i].webport;
for (var ij = 0; ij< mqttservers.length; ij++)
{
$("#mq"+ij).css("background-color", "red");
}
$("#mq"+i).css("background-color", "green");
}
function mqttconnect()
{
// Connect to Public Broker
try
{
client = new Messaging.Client(mqttserver,
mqttwebport, "clientId_"
+ parseInt(Math.random() * 100, 10));
client.connect(options);
client.onMessageArrived = function(message)
{
isens++;
$("#mqttout").append(
"onMessageArrived: " + isens + " Topic: "
+ message.destinationName + ' | '
+ message.payloadString + "<br />");
}
}
catch (e)
{
$("#mqttout").html("mqttconnect: Error - " + e.message);
}
}
function mqttpublish(num)
{
try
{
num = num || 1;
$("#mqttout").append("publish: Testdata sent" + "<br />");
if (client != null)
{
for (i = 0; i < num; i++)
{
var act = "A";
if (i % 2 == 0)
act = "G";
var sensdata = act + ";" + new Date().getTime() + ";"
+ Math.random() * 10 + ";" + Math.random() * 10 + ";"
+ Math.random() * 10 + ";";
var message = new Messaging.Message(sensdata);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
}
else
{
$("#mqttout").append("publish: Not connected" + "<br />");
}
}
catch (e)
{
$("#mqttout").html("mqttpublish: Error - " + e.message);
}
}
function mqttdisconnect()
{
try
{
if (client != null)
{
client.disconnect();
$("#mqttout").append("mqttdisconnect: Disconnected" + "<br />");
client = null;
}
else
{
$("#mqttout").append("mqttdisconnect: Not connected" + "<br />");
}
}
catch (e)
{
$("#mqttout").html("mqttdisconnect: Error - " + e.message);
}
}