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

梦想与她 提交于 2019-12-30 19:15:02

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了

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!