微信小程序使用mqtt.js支持wss

mqtt也可以支持wss,具体如何操作:

  1. 服务端配置
    在服务器上装个nginx,nginx.conf配置如下:
    因为我用的是rabbitmq的rabbitmq_web_mqtt,所以我的配置路径是下面的地址,也可根据自己的mq地址来
 location /mqttwss {
     proxy_set_header Host $http_host;
     proxy_pass http://127.0.0.1:xxx/ws;
     proxy_http_version 1.1;
     proxy_set_header X-Client-IP $remote_addr;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     proxy_read_timeout 300s; # 默认是60秒,可设置
 }

客户端配置:

var mqtt = require('mqtt')
let option={
	host: "www.xxx.xxx",
	port: xx,
	 keepalive: 15,
	 username: "username",
	 password: "password",
	 port:443,
	 path:"/mqttwss"
}
let mqttClient = mqtt.connect("wxs://"+option.host+":"+option.port+"/mqttwss", option)

这样就支持wss了

你可能感兴趣的:(#,uni,#,小程序)