主要库:https://github.com/mqttjs/MQTT.js
一、搭建EMQX
略;
二、配置nginx
server{ listen 443; server_name www.com.cn; proxy_intercept_errors on; recursive_error_pages on; ssl on; ssl_certificate /etc/letsencrypt/live/www.com.cn/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.com.cn/privkey.pem; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; client_max_body_size 100m; location / { proxy_pass http://127.0.0.1:8080; } location /mqtt { proxy_pass http://127.0.0.1:8083; proxy_redirect off; # proxy_set_header Host www.domain.com:8083; proxy_set_header Sec-WebSocket-Protocol mqtt; # more_clear_headers Sec-WebSocket-Protocol; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
-重启Nginx
三、Nodejs Demo
var mqtt = require('mqtt') var client = mqtt.connect('wss://www.com.cn/mqtt') client.publish('/World', 'Hello mqtt from wss') client.subscribe("/World") client.on("message", function (topic, payload) { console.log(payload.toString()) })
参考连接:
https://segmentfault.com/a/1190000012865251