微信小程序使用WebSocket的坑
http 与 https 协议
使用http协议,websocket的url,应该是ws开头,如:ws://abc.cn/websocket/infos
使用https协议,websocket的url,应该是wss开头,如:wss://abc.cn/websocket/infosnginx使用https反向代理tomcat
nginx的443端口反向代理tomcat,需要在conf文件的header上面增加以下参数
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";顺便贴一下,微信小程序项目的完整的https协议的nginx反向代理配置
server { listen 443 ssl; server_name abc.xxx.cn; ssl on; ssl_certificate cert/ssl/abc.xxx.cn.pem; ssl_certificate_key cert/ssl/abc.xxx.cn.key; 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;proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme; # 微信小程序推送配置校验文件路径location /123.txt {proxy_pass https://127.0.0.1:8443/static/123.txt;}location /{include uwsgi_params;proxy_pass https://127.0.0.1:8443; }}- 在开发工具调试websocket没有问题,使用真机测试一直pending的问题
查了半天,才发现原来我的websocket路径,被shiro拦截了,在开发工具有会话session所以没有问题,但是真机调试没有会话session所以一直被拦截无法连接。在shiro配置上面取消对websocket拦截即可。【Ps:暂时还没研究到如何在小程序的websocket带token】
参考链接
【请求头出现Provisional headers are shown】
【webSocket连不上后端,webSocket被因为没有token拦截了】
【websocket里面添加Token】
【微信小程序WebSocket相关问题说明】














