首页 > PHP技术 > swoole > swoole SSL加密示例分享
2021
06-10

swoole SSL加密示例分享

平时太忙对业务方面的安全方面考虑还是不够细致,此前的项目就遇到了这样的问题,对于数据安全验证不够严谨,今天发现swoole 自带了ssl加密,于是实验了一下就拿出来分享:



<?php

/**

* File Description

* User: Kp

* Date: 2015/10/19

* Time: 15:08

*/

class TcpServer{

   private static $instance;

   public function __construct(){

       $serv = new swoole_server("0.0.0.0", 9501 , SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);

       $serv->set(array(

           'worker_num'        => 8,   //工作进程数量

           'daemonize'         => false, //是否作为守护进程

           'ssl_key_file'      => __DIR__.'/ssl/ssl.key',

           'ssl_cert_file'     => __DIR__.'/ssl/ssl.crt',

       ));

       $serv->on('Connect', array($this , 'onConnect'));

       $serv->on('Receive', array($this , 'onReceive'));

       $serv->on('Close', array($this , 'onClose'));

       $serv->start();

   }

   public function onConnect(swoole_server $serv , $fd){

       echo 'Client:'.$fd."\n";

   }

   public function onReceive(swoole_server $serv, $fd, $from_id, $data){

       echo 'Receive:'.$fd."\n";

   }

   public function onClose(swoole_server $serv , $fd){

       echo "Close: $fd.\n";

   }

   public static function getInstance(){

       if(!isset(self::$instance)){

           self::$instance = new self();

       }

       return self::$instance;

   }

}

TcpServer::getInstance();



当客户端发送数据时未带证书信息,连接直接被中断。这样就使得系统更加地安全。

以上就是“Swoole SSL加密示例分享”的详细内容,想要了解更多swoole相关资讯或者教程欢迎关注编程学习网

扫码程序猿 获取免费致富资料

编程学习

查 看2019高级编程视频教程免费获取