Thinkphp实用技巧【TP5.1中swoole开启SSL支持(wss连接)】

1.首先找到网站根目录下/vendor/topthink/think-swoole/src/Server.php中75行,即:public function __construct(),添加ssl服务类型

    public function __construct()
    {
        // 实例化 Swoole 服务
        switch ($this->serverType) {
            case 'socket':
                $this->swoole = new Websocket($this->host, $this->port, $this->mode, $this->sockType);
                break;
            case 'http':
                $this->swoole = new HttpServer($this->host, $this->port, $this->mode, $this->sockType);
                break;
            // 增加ssl类型服务
            case 'ssl':
                $this->swoole = new Websocket($this->host, $this->port, $this->mode, $this->sockType);
                break;
            default:
                $this->swoole = new SwooleServer($this->host, $this->port, $this->mode, $this->sockType);
        }
        //...
    }

2.在配置文件swoole_server.php修改配置

// +----------------------------------------------------------------------
// | Swoole设置 php think swoole:server 命令行下有效
// +----------------------------------------------------------------------
return [
    // 扩展自身配置
    'host'         => '0.0.0.0', // 监听地址
    'port'         => 9508, // 监听端口
    'type'         => 'socket', // 服务类型 支持 socket http server
    'mode'         => '', // 运行模式 默认为SWOOLE_PROCESS
    // 'sock_type'    => '',// 默认为SWOOLE_SOCK_TCP 
    'swoole_class' => '', // 自定义服务类名称
    'sock_type'    => SWOOLE_SOCK_TCP | SWOOLE_SSL, // sock type 默认为SWOOLE_SOCK_TCP
    'serverType'   => 'ssl',
    'ssl_key_file' => '/www/server/panel/vhost/ssl/XXXXX/privkey.pem', 
    'ssl_cert_file' => '/www/server/panel/vhost/ssl/XXXXX/fullchain.pem',
    
 
    // 可以支持swoole的所有配置参数
    'daemonize'    => false,    //守护进程
    'pid_file'     => Env::get('runtime_path') . 'swoole_server.pid',
    'log_file'     => Env::get('runtime_path') . 'swoole_server.log',
    'worker_num'   => 3,  //进程数
    
    //心跳检测:每60秒遍历所有连接,强制关闭10分钟内没有向服务器发送任何数据的连接
    'heartbeat_check_interval' => 10,
    'heartbeat_idle_time' => 60,
    'buffer_output_size' => 4*1024*1024, // 发送输出缓存区内存
    'socket_buffer_size' => 4*1024*1024, // 配置客户端连接的缓存区长度
    //...
];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值