美文网首页
从零开始搭建聊天系统之如何开启ws与wss协议

从零开始搭建聊天系统之如何开启ws与wss协议

作者: 隔岸坐看云卷云舒 | 来源:发表于2019-03-22 12:39 被阅读0次

    没有看搭建websocket的请移步从零搭建聊天系统之Lumen+GatewayWorker
    下面请看继承了Command类的SocketServer的代码

    <?php
    /*
        *author:mjshu
        *time 2017/9/27 上午11:24
        *All rights reserved
    */
    
    
    namespace App\Console\Commands;
    
    
    use GatewayWorker\BusinessWorker;
    use GatewayWorker\Gateway;
    use GatewayWorker\Register;
    use Illuminate\Console\Command;
    use Workerman\Worker;
    
    class SocketServer extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'work:socket {action} {--d}';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'workerman socket';
    
        /**
         * SocketServer constructor.
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * @author mjShu
         */
        public function handle()
        {
            global $argv;
            $action = $this->argument('action');
            $argv[0]='worker:socket';
            $argv[1]=$action;
            $argv[2]=$this->option('d')?'-d':'';
    
    //        $context = array(
    //            'ssl' => array(
    //                'local_cert'  => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.crt', // 或者crt文件
    //                'local_pk'    => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.key',
    //                'verify_peer' => false
    //            )
    //        );
    //        $Gateway = new Gateway('websocket://0.0.0.0:9006',$context);
    //        $Gateway->transport = 'ssl';
    
            $Gateway = new Gateway('websocket://0.0.0.0:8082');
    
            new Register('text://0.0.0.0:1238');
            $Gateway->name = 'Gateway';
            $Gateway->count = 1;
            $Gateway->lanIp = '127.0.0.1';
            $Gateway->startPort = 10000;
            $Gateway->pingInterval = 10;  //10s一次心跳
            $Gateway->registerAddress = '127.0.0.1:1238';
            $Gateway->pingNotResponseLimit = 3;
            $Gateway->pingData = '';
            $worker = new BusinessWorker();
            $worker->eventHandler = 'App\Http\Controllers\WebSocket\WebSocketController';
            $worker->name = 'BusinessWorker';
            $worker->count = 3;
            $worker->registerAddress = '127.0.0.1:1238';
            Gateway::runAll();
        }
    }
    

    那么ws和wss有什么区别,wss即在ws协议上做了一层加密,在代码中我们可以看到

    //        $context = array(
    //            'ssl' => array(
    //                'local_cert'  => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.crt,', // 或者crt文件
    //                'local_pk'    => '/home/wwwroot/im.caomei520.com/ssl/im.caomei520.com.key',
    //                'verify_peer' => false
    //            )
    //        );
    //        $Gateway = new Gateway('websocket://0.0.0.0:9006',$context);
    //        $Gateway->transport = 'ssl';
    

    在local_cert中就是你申请证书的公钥,local_pk即为你的私钥
    我们在实例化Gateway中传入$context参数并开启ssl

    $gateway->transport = 'ssl';
    

    如果不需要走wss协议直接实例化Gateway不用传递$context,那么默认就是ws协议

    相关文章

      网友评论

          本文标题:从零开始搭建聊天系统之如何开启ws与wss协议

          本文链接:https://www.haomeiwen.com/subject/uhplvqtx.html