我已经实现了一个角度的paho mqtt,用于来自AWS IOT主题的实时数据流。数据在桌面浏览器和android webview中出现。我还处理了网络断开连接和重新连接或aws身份验证令牌过期的情况。
这一切都在桌面浏览器上工作,网络断开连接停止数据更新,同时重新连接再次发送数据到页面。但是,断开并重新连接网络后,andoid webview无法将数据发送到页面。
这是mqtt连接的角度代码。
initClient(requestUrl: string) {
const clientId = String(Math.random()).replace('.', '');
const client = new Paho.MQTT.Client(requestUrl, clientId);
const that = this;
const connectOptions = {
onSuccess: function () {
console.log('connected');
// subscribe to the topic
client.subscribe('topicname', {});
},
useSSL: true,
keepAliveInterval: 60 * 60,
timeout: 10,
mqttVersion: 4,
onFailure: function (err: any) {
that.isError = true;
console.log(err);
console.error('connect failed' + err);
}
};
client.onConnectionLost = (responseObject: any) => {
if (responseObject.errorCode !== 0) {
console.log(' onConnectionLost:' + responseObject.errorCode + ' -- ' + responseObject.errorMessage);
that.authenticateAWS();
}
};
client.connect(connectOptions);
// this.locations = [];
// let i = 0;
// setInterval(() => this.processMessage({ "payloadString" : this.testdata(i++)}), 10000);
client.onMessageArrived = (message) => {
this.processMessage(message);
};
}
我可以通过webview apis在android logcat上获得以下控制台错误。
[INFO:CONSOLE(1)] " onConnectionLost:8 -- AMQJS0008I Socket closed.", source: http://PAGEURL/vendor.hash.bundle.js (1)
[INFO:CONSOLE(1)] "WebSocket connection to 'wss://IOTURL' failed: Error in connection establishment: net::ERR_INTERNET_DISCONNECTED", source: http://PAGEURL/main.hash.bundle.js (1)
[INFO:CONSOLE(1)] "[object Object]", source: http://PAGEURL/vendor.hash.bundle.js (1)
[INFO:CONSOLE(1)] "connect failed[object Object]", source: http://PAGEURL/vendor.hash.bundle.js (1)
此后数据流在webview中停止。
但桌面浏览器在重新连接并继续数据传输后才会出现以下错误。
onConnectionLost:8 -- AMQJS0008I Socket closed.
更新:桌面Firefox浏览器也无法重新连接以下错误。
The connection to wss://IOTURL was interrupted while the page was loading.
onConnectionLost:7 -- AMQJS0007E Socket error:undefined.