前言
微信小程序支持使用 WebSocket 连接到服务器,准确地说是带 SSL 的 WebSocket,而微信小程序中不允许使用带端口的 wss 连接,只能使用 443 端口。想使用其他端口就需要在服务器做一层代理,本文以 Ubuntu 16.04 服务器为例,使用 nginx 做 Web Server 。本文参考了 如何在微信小程序的websocket上使用mqtt协议 ,在此感谢原作者。
步骤
安装 nginx 及配置的过程不再赘述,nginx 需要处理微信小程序 WebSocket 不支持 Sec-WebSocket-Protocol 头的问题,默认 nginx 不带这个功能,需要加上补丁 headers-more-nginx-module 后重新编译。Ubuntu 16.04 的 nginx 版本为 1.10.3 。
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar zxvf nginx-1.10.3.tar.gz
git clone https://github.com/openresty/headers-more-nginx-module.git
cp -r headers-more-nginx-module nginx-1.10.3
cd ng