node client_test.js
を使用すると、以下のコードがうまく機能します。Reactjs mqtt client urlがwssを返すのはなぜですか?
(unknown) WebSocket connection to 'wss://dev.xxx.com:8083/' failed: Error in connection establishment: net::ERR_INSECURE_RESPONSE
は、このURLは、常にwss
プロトコルに変換します。私はreactjsにこれを置いたときしかし、私はこのエラーを取得します。なぜこのようなことが起こるのか分かりません。 ReactJSがブラウザで実行されるため
var mqtt = require('mqtt')
var options = {
//port: 8083,
//host: 'dev.xxx.com',
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
username: 'xxx',
password: new Buffer('xxx'),
keepalive: 10,
reconnectPeriod: 1000,
protocolId: 'MQIsdp',
protocolVersion: 3,
clean: true,
encoding: 'utf8',
//protocol: 'mqtts',
rejectUnauthorized : false,
will: {
topic: 'node/status',
payload: new Buffer('offline')
}
};
var client = mqtt.connect('mqtts://dev.xxx.com:8083', options)
client.on('connect', function() {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
})
ありがとう、@有用な答え –