在網上查了多資料,花了2天的時間終于把微信小程的聊天室環境搭建起來了,還算滿意,在這裡和大家分享下,希望能幫助要幫助的小程式開發者。
和網上介紹相同: nginx反向代理, 小程式mqtt插件 paho-mqtt.js, 用到mqtt伺服器,
1. nignx,
nginx安裝網上都有,說下注意的幾點:
a. 小程式wss必須用到443 端口(目前我是這麼做的,用其他端口,小程式會報錯,不是有效的https什麼的,忘記截圖了)
b. nginx 安裝我使用的是源碼編譯安裝,開始用的yum安裝,發現沒有./configure 沒法添加第三方子產品
c. 需要添加第三方子產品, 在這裡直接貼出我的子產品配置
第三方子產品位址:https://www.nginx.com/resources/wiki/modules/
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=./mnt/headers-more-nginx-module-0.33
d. 小程式支援mqtt , 通過nginx配置:
server {
listen 443 ssl;
server_name localhost1 localhost2 ;
ssl_certificate *.pem;
ssl_certificate_key *.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1;
}
location = /mqtt {
proxy_pass http://127.0.0.1:8083;
proxy_redirect off;
proxy_set_header Host 127.0.0.1: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";
}
}
2. paho-mqtt.js
通過網上參考的例子: https://github.com/tennessine/paho.mqtt.wxapp;
介紹的很詳細,就不多說了, 有一點,說下,開發中,沒有太多時間深入研究,
a. 在用443端口連結時間,小程成依舊報錯,我就把原js所有關于端口拼接的先注釋掉了,就可以了,如果有知道的,可以告訴我,謝謝哦,
b. 開發中 在頁面中定義client 進入退出頁面,再次進傳入連結接不上mqtt, 也不知什麼原因,最後沒法辦,把client定義成全局的
ok