emqx ws转成wss
主要是在nginx里加
把8083转成443
下面是完整nginx配置
server { listen 443; server_name localhost; root html; index index.html index.htm; ssl on; ssl_certificate /root/hserver/ssl/XXXXXX.pem; ssl_certificate_key /root/hserver/ssl/XXXXXX.key; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_timeout 5m; location / { root html; index index.html index.htm; proxy_pass http://localhost:3000; } location /mqtt { access_log //mnt/logs/emqtt.log; proxy_pass http://localhost:8083/mqtt; proxy_read_timeout 60s; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-for $remote_addr; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection Upgrade; } }
下面是客户端测试的东西:
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> <script> // 将在全局初始化一个 mqtt 变量 //console.log(mqtt) // 连接选项 const options = { connectTimeout: 4000, // 超时时间 // 认证信息 clientId: 'emqx-connect-via-webstest', username: 'user', password: 'pass', } const client = mqtt.connect('wss://yun.xxxx.cn/mqtt', options) client.on('connect', (e) => { console.log('成功连接服务器') // 订阅一个主题 client.subscribe('hello', { qos: 1 }, (error) => { if (!error) { console.log('订阅成功'); client.publish('hello', 'Hello EMQ', { qos: 1, rein: false }, (error) => { console.log(error || '发布成功') }) } }) }) </script>