Description
configurations:
broker.xml:
<acceptor name="mqtttls">tcp://0.0.0.0:1883?sslEnabled=true;sslProvider=OPENSSL;keyStorePath=/data/mqtt_ssl/mqtt_broker.keystore;keyStorePassword=12345;trustStorePath=/data/mqtt_ssl/mqtt_broker.truststore;trustStorePassword=12345;needClientAuth=true;tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true;producerMaxRate=5;handshake-timeout=120</acceptor>
testPage.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>MQTT over WebSocket</title> <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> </head> <body> </body> <script> const clientId = 'mqttjs_' + Math.random().toString(16).substring(2, 8) const connectUrl = 'wss://xxxxx:1883' const options = { keepalive: 30, clientId: clientId, clean: true, connectTimeout: 5000, username: 'test', password: 'test', reconnectPeriod: 1000, key: KEY, cert: CERT, ca: CA_CERT, } const topic = '/WebSocket/mqtt' const payload = 'WebSocket mqtt test' const qos = 0 console.log('connecting mqtt client') const client = mqtt.connect(connectUrl, options) client.on('connect', () => { console.log('Client connected:' + clientId) client.subscribe(topic, { qos }, (error) => { if (error) { console.log('Subscribe error:', error) return } console.log(`Subscribe to topic ${topic}`) } ) </script> </html>
Run error:
The server always auto disconnect the connection