创建 wss 服务
准备工作:
1、Workerman 版本不小于 3.3.7
2、PHP 安装了 openssl 扩展
3、已经申请了证书(pem/crt 文件及 key 文件)放在磁盘某个目录 (位置任意)
start_gateway.php 中设置以下代码。
php
// 证书最好是申请的证书
$context = array(
// 更多ssl选项请参考手册 https://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 请使用绝对路径
'local_cert' => '磁盘路径/server.pem', // 也可以是crt文件
'local_pk' => '磁盘路径/server.key',
'verify_peer' => false,
// 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
)
);
// websocket协议(端口任意,只要没有被其它程序占用就行)
$gateway = new Gateway("websocket://0.0.0.0:443", $context);
// 开启SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';
注意:
1、如果无法启动,则一般是 443 端口被占用,请改成其它端口。如果必须使用 443 端口请参考 workerman 手册创建 wss 服务方法二部分。
2、wss 端口只能通过 wss 协议访问,ws 无法访问 wss 端口。
3、证书一般是与域名绑定的,所以测试的时候请使用证书对应的域名去连接,不要使用其它域名或者 ip 去连。
4、如果出现无法访问的情况,请检查服务器防火墙。
5、此方法要求 PHP 版本 >=5.6,因为微信小程序要求 tls1.2,而 PHP5.6 以下版本不支持 tls1.2。
更多 wss 相关信息参考 workerman 手册创建 wss 服务